机器人 - 如何为preferenceActivity自定义布局中的Andr​​oid 3.0? [英] android - How to set custom layout for PreferenceActivity in Android 3.0?

查看:149
本文介绍了机器人 - 如何为preferenceActivity自定义布局中的Andr​​oid 3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用的minSdkVersion =11的应用程序,该应用程序是为平板电脑和Android 4.0及更高版本。我曾经审议关于这一主题的互联网,但还没有找到太多。

要实现对Android SDK中的previous版本的自定义布局,我们只需要使用的ListView创建布局(比如preference.xml)和其ID等于android.R.id.list和使用的setContentView方法。

preference.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>
    < ListView的机器人:ID =@机器人:ID /列表
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =FILL_PARENT/>
< / RelativeLayout的>
 

在的Andr​​oid 3.0的事情发生了变化,preferences与利用片段来实现。这就是我如何preference_headers.xml文件看起来像:

 < preference报头
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <头安卓碎片=com.example.My preferenceActivity $ GeneralSettingsFragment
            机器人:标题=常规
            机器人:总结=常用设置。 />
    <头
            机器人:标题=Example.com>
        <意图安卓行动=android.intent.action.VIEW
                机器人:数据=htt​​p://www.example.com/>
    < /头>

< / preference报头>
 

我的preferenceActivity.java:

 公共类我的preferenceActivity扩展preferenceActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout preference。);
        //添加一个按钮,标题列表。
        如果(hasHeaders()){
            Button按钮=新的按钮(这一点);
            button.setText(注销);
            setListFooter(按钮);

        }

    }


    / **
     *填充活性顶层头。
     * /
    @覆盖
    公共无效onBuildHeaders(名单<头>目标){
        loadHeadersFromResource(R.xml preference_headers,目标);
    }

    公共静态类GeneralSettingsFragment扩展preferenceFragment {
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);

            //从XML资源负载的preferences
            加preferencesFromResource(R.xml.fragmented_ preferences);

        }
    }
}
 

现在,如果我跑我的preferenceActivity我会看在LogCat中此错误:

 > java.lang.IllegalArgumentException:如果没有查看发现ID 0x10202be的
>片段GeneralSettingsFragment {4077f8c0#0 ID = 0x10202be}
> E / AndroidRuntime(17103):在
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:729)
> E / AndroidRuntime(17103):在
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
> E / AndroidRuntime(17103):在
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909)
> E / AndroidRuntime(17103):在
> android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1584)
> E / AndroidRuntime(17103):在
> android.app.Activity.performStart(Activity.java:4377)
> E / AndroidRuntime(17103):在
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1724)
> E / AndroidRuntime(17103):11 ...更多
 

我知道是什么原因导致这个问题。 FragmentManager只是找不到一个视图中插入片段GeneralSettingsFragment。但是我不知道如何解决这个问题。

顺便说一句,如果我运行在Android 4.0相同的应用程序,那么我就可以看到第一个preference活性的头。但是,如果我点击常规,程序会崩溃,我会收到LogCat中类似的错误:

  java.lang.IllegalArgumentException:如果没有查看发现ID 0x10202cd的片段GeneralSettingsFragment {4134b4e0#0 ID = 0x10202cd}
E / AndroidRuntime(2170):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:789)
E / AndroidRuntime(2170):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
E / AndroidRuntime(2170):在android.app.BackStackRecord.run(BackStackRecord.java:622)
E / AndroidRuntime(2170):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
E / AndroidRuntime(2170):在android.app.Activity.performStart(Activity.java:4474)
E / AndroidRuntime(2170):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
 

解决方案

我有同样的问题,而不是你。

我尝试了很多东西,但这里是我的全部code:

<一个href="https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/My$p$pferenceActivity.java" rel="nofollow">https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/My$p$pferenceActivity.java

关键是要增加的setContentView在onBuildHeaders部分,但不是在OnCreate。

我也取得了一些片段,测试和不完全知道为什么,这是工作,但我答应你!我已经在平板电脑上标题部分中的自定义布局

<一个href="https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/view/Stock$p$pferenceFragment.java" rel="nofollow">https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/view/Stock$p$pferenceFragment.java

I am developing app with minSdkVersion="11", that is app for tablets and Android 4.0 and newer. I have scrutinized internet on this topic, but have not found much.

To implement custom layout for previous versions of Android SDK we just have to create layout (say preference.xml) with ListView and its id equals to android.R.id.list and use setContentView method.

preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" > 
    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" /> 
</RelativeLayout>

In Android 3.0 things have changed and Preferences are implemented with use of fragments. That is how my preference_headers.xml file looks like:

<preference-headers
        xmlns:android="http://schemas.android.com/apk/res/android">

    <header android:fragment="com.example.MyPreferenceActivity$GeneralSettingsFragment" 
            android:title="General"
            android:summary="Common settings." />
    <header  
            android:title="Example.com" >
        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.example.com" />
    </header> 

</preference-headers>

MyPreferenceActivity.java:

public class MyPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.preference);
        // Add a button to the header list.
        if (hasHeaders()) {
            Button button = new Button(this); 
            button.setText("Log out");
            setListFooter(button);

        }

    }


    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    public static class GeneralSettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_preferences);

        }
    }
}

Now if I run MyPreferenceActivity I will see this error in LogCat:

> java.lang.IllegalArgumentException: No view found for id 0x10202be for
> fragment GeneralSettingsFragment{4077f8c0 #0 id=0x10202be}
> E/AndroidRuntime(17103): at
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:729)
> E/AndroidRuntime(17103): at
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:926)
> E/AndroidRuntime(17103): at
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:909)
> E/AndroidRuntime(17103): at
> android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1584)
> E/AndroidRuntime(17103): at
> android.app.Activity.performStart(Activity.java:4377)
> E/AndroidRuntime(17103): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1724)
> E/AndroidRuntime(17103):  ... 11 more

I know what causes this problem. FragmentManager just cannot find a view to insert fragment GeneralSettingsFragment in. But I don't know how to solve it.

By the way, if I run the same app on Android 4.0, then I can see the first Preference Activity with headers. But if I click on General, app will crash and I will receive similar error in LogCat:

java.lang.IllegalArgumentException: No view found for id 0x10202cd for fragment GeneralSettingsFragment{4134b4e0 #0 id=0x10202cd}
E/AndroidRuntime(2170):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:789)
E/AndroidRuntime(2170):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
E/AndroidRuntime(2170):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
E/AndroidRuntime(2170):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
E/AndroidRuntime(2170):     at android.app.Activity.performStart(Activity.java:4474)
E/AndroidRuntime(2170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)

解决方案

I had the same issue than you.

I tried a lot of stuff but here is my full code:

https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/MyPreferenceActivity.java

The trick is to add the setContentView in the onBuildHeaders section, but NOT in the onCreate.

I also made some tests in fragment and not perfectly sure why this is working, but I promise you: I have a custom layout on tablet header section!

https://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/view/StockPreferenceFragment.java

这篇关于机器人 - 如何为preferenceActivity自定义布局中的Andr​​oid 3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆