Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面

Using Preference Headers



In rare cases, you might want to design your settings such that the first screen displays only a list of subscreens(such as in the system Settings app, as shown in figures 4 and 5). When you‘re developing such a design for Android 3.0 and higher, you should use a new "headers" feature in Android 3.0, instead of building subscreens with nested PreferenceScreen elements.

To build your settings with headers, you need to:

  1. Separate each group of settings into separate instances of PreferenceFragment. That is, each group of settings needs a separate XML file.
  2. Create an XML headers file that lists each settings group and declares which fragment contains the corresponding list of settings.
  3. Extend the PreferenceActivity class to host your settings.
  4. Implement the onBuildHeaders() callback to specify the headers file.

A great benefit to using this design is that PreferenceActivity automatically presents the two-pane layout shown in figure 4 when running on large screens.

Even if your application supports versions of Android older than 3.0, you can build your application to usePreferenceFragment for a two-pane presentation on newer devices while still supporting a traditional multi-screen hierarchy on older devices (see the section about Supporting older versions with preference headers).

Figure 4. Two-pane layout with headers. 
1. The headers are defined with an XML headers file. 
2. Each group of settings is defined by a PreferenceFragment that‘s specified by a <header> element in the headers file.

Figure 5. A handset device with setting headers. When an item is selected, the associated PreferenceFragmentreplaces the headers.

Creating the headers file

Each group of settings in your list of headers is specified by a single <header> element inside a root<preference-headers> element. For example:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header
        android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentOne"
        android:title="@string/prefs_category_one"
        android:summary="@string/prefs_summ_category_one" />
    <header
        android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentTwo"
        android:title="@string/prefs_category_two"
        android:summary="@string/prefs_summ_category_two" >
        <!-- key/value pairs can be included as arguments for the fragment. -->
        <extra android:name="someKey" android:value="someHeaderValue" />
    </header>
</preference-headers>

With the android:fragment attribute, each header declares an instance of PreferenceFragment that should open when the user selects the header.

The <extras> element allows you to pass key-value pairs to the fragment in a Bundle. The fragment can retrieve the arguments by calling getArguments(). You might pass arguments to the fragment for a variety of reasons, but one good reason is to reuse the same subclass of PreferenceFragment for each group and use the argument to specify which preferences XML file the fragment should load.

For example, here‘s a fragment that can be reused for multiple settings groups, when each header defines an<extra> argument with the "settings" key:

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

        String settings = getArguments().getString("settings");
        if ("notifications".equals(settings)) {
            addPreferencesFromResource(R.xml.settings_wifi);
        } else if ("sync".equals(settings)) {
            addPreferencesFromResource(R.xml.settings_sync);
        }
    }
}

Displaying the headers

To display the preference headers, you must implement the onBuildHeaders() callback method and callloadHeadersFromResource(). For example:

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }
}

When the user selects an item from the list of headers, the system opens the associated PreferenceFragment.

Note: When using preference headers, your subclass of PreferenceActivity doesn‘t need to implement theonCreate() method, because the only required task for the activity is to load the headers.

Supporting older versions with preference headers

If your application supports versions of Android older than 3.0, you can still use headers to provide a two-pane layout when running on Android 3.0 and higher. All you need to do is create an additional preferences XML file that uses basic <Preference> elements that behave like the header items (to be used by the older Android versions).

Instead of opening a new PreferenceScreen, however, each of the <Preference> elements sends an Intentto the PreferenceActivity that specifies which preference XML file to load.

For example, here‘s an XML file for preference headers that is used on Android 3.0 and higher (res/xml/preference_headers.xml):

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header
        android:fragment="com.example.prefs.SettingsFragmentOne"
        android:title="@string/prefs_category_one"
        android:summary="@string/prefs_summ_category_one" />
    <header
        android:fragment="com.example.prefs.SettingsFragmentTwo"
        android:title="@string/prefs_category_two"
        android:summary="@string/prefs_summ_category_two" />
</preference-headers>

And here is a preference file that provides the same headers for versions older than Android 3.0 (res/xml/preference_headers_legacy.xml):

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:title="@string/prefs_category_one"
        android:summary="@string/prefs_summ_category_one"  >
        <intent
            android:targetPackage="com.example.prefs"
            android:targetClass="com.example.prefs.SettingsActivity"
            android:action="com.example.prefs.PREFS_ONE" />
    </Preference>
    <Preference
        android:title="@string/prefs_category_two"
        android:summary="@string/prefs_summ_category_two" >
        <intent
            android:targetPackage="com.example.prefs"
            android:targetClass="com.example.prefs.SettingsActivity"
            android:action="com.example.prefs.PREFS_TWO" />
    </Preference>
</PreferenceScreen>

Because support for <preference-headers> was added in Android 3.0, the system calls onBuildHeaders() in your PreferenceActivity only when running on Androd 3.0 or higher. In order to load the "legacy" headers file (preference_headers_legacy.xml), you must check the Android version and, if the version is older than Android 3.0 (HONEYCOMB), call addPreferencesFromResource() to load the legacy header file. For example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        // Load the legacy preferences headers
        addPreferencesFromResource(R.xml.preference_headers_legacy);
    }
}

// Called only on Honeycomb and later
@Override
public void onBuildHeaders(List<Header> target) {
   loadHeadersFromResource(R.xml.preference_headers, target);
}

The only thing left to do is handle the Intent that‘s passed into the activity to identify which preference file to load. So retrieve the intent‘s action and compare it to known action strings that you‘ve used in the preference XML‘s <intent> tags:

final static String ACTION_PREFS_ONE = "com.example.prefs.PREFS_ONE";
...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String action = getIntent().getAction();
    if (action != null && action.equals(ACTION_PREFS_ONE)) {
        addPreferencesFromResource(R.xml.preferences);
    }
    ...

    else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        // Load the legacy preferences headers
        addPreferencesFromResource(R.xml.preference_headers_legacy);
    }
}

Beware that consecutive calls to addPreferencesFromResource() will stack all the preferences in a single list, so be sure that it‘s only called once by chaining the conditions with else-if statements.

时间: 2024-11-06 07:21:57

Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面的相关文章

如何在Android中为TextView动态设置drawableLeft等

如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 1 Drawable drawable = getResources().getDrawable(resId); 2 //注意查看方法TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) 3 //的注释,要求设置的drawable必须已经通过Drawable.setBounds方法设置过边界参数 4

android中ColorStateList及StateListDrawable设置Selector

写过android的代码相信大家对Selector并不陌生吧,下面来看看这段xml文件是如何定义的 <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 触摸时并且当前窗口处于交互状态 --> <item android:state

Android源码AOSP之设置Settings阅读记录

Android 4.4 系统的设置源码阅读记录 ----------2014-7-3------------------ AndroidManifest.xml launch的activity是 Settings,另外有40多个activity继承于它,比如设置的一级菜单: wifi,蓝牙,声音,显示,安全,应用程序,语言和时间,关于设备等等.实际上都是这一个acitivy. 这里从安全设置看起,SecuritySettings.java 以资源文件R.xml.security_settings

Android中通过反射来设置显示时间

这个Toast的显示在Android中的用途还是很大的,同时我们也知道toast显示的时间是不可控的,我们只能修改他的显示样式和显示的位置,虽然他提供了一个显示时间的设置方法,但是那是没有效果的(后面会说到),他有两个静态的常量Toast.SHORT和Toast.LONG,这个在后面我会在源码中看到这个两个时间其实是2.5s和3s.那么我们如果真想控制toast的显示时间该怎么办呢?真的是无计可施了吗?天无绝人之路,而且Linux之父曾经说过:遇到问题就去看那个操蛋的源代码吧!!下面就从源代码开

简单分析android textview xml 的属性设置

android:ems 设置TextView的宽度为N个字符的宽度. 这样的好处就是,在定义编辑框空间输入多少字符的时候,可以根据固定的值设置编辑框宽度.保证边框和文字的宽度统一.android:maxems 设置TextView的宽度为最长为N个字符的宽度.与ems同时使用时覆盖ems选项. 一搬也是在控制文字的数据的个数上做了一定的限制android:minems 设置TextView的宽度为最短为N个字符的宽度.与ems同时使用时覆盖ems选项. 同上android:maxLength 限

【原创】如何在Android中为TextView动态设置drawableLeft等

如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 1 Drawable drawable = getResources().getDrawable(resId); 2 //注意查看方法TextView.setCompoundDrawables(Drawable, Drawable, Drawable, Drawable) 3 //的注释,要求设置的drawable必须已经通过Drawable.setBounds方法设置过边界参数 4

Android Studio显示主题/样式设置

估计很多刚开始用Android Studio的DEV,都有经常看到网上关于Android Studio的贴图是灰色样式的,但是为啥自己刚安装的就是白色样式的呢. 这个其实只要改下显示主题就可以了. 如下图,选择Darcula就可以了,IntelliJ是默认风格,Windows这个风格其实颜色和IntelliJ是差不多的: Android Studio显示主题/样式设置,布布扣,bubuko.com

Android数据库SQLite表内设置外键

Android数据库SQLite表内设置外键 介绍 Android默认的数据是SQLite,但SQLite3.6.19之前(在2.2版本中使用的是3.6.22,因此如果你的应用只兼容到2.2版本就可以放心使用外键功能)是不支持外键的,如果有两张表需要关联,用外键是最省事的,但不支持的话怎么办呢?这里就有一个解决办法,就是用事务将两张表关联起来,并且最后生成一张视图. 现有两张表 Employees Dept 视图 ViewEmps:显示雇员信息和他所在的部门 创建数据库 自定义一个辅助类继承SQ

Android Studio的几个设置

1.主题. File->Settings, 搜索Theme, 点开Appearance, 在右边面板的Theme中选择Darcula. 2.注释快捷键. File->Settings,搜索keymap,点开Keymap,在右边面板上搜索,找到Other->Fix doc comment,双击,选择Add Keyboard Shortcut,按下你的快捷键.Eclipse中是alt + shift +J,但是在这里会冲突,所以我用了比较近的快捷键组合:alt + shift + K.在修改

Android: EditText设置属性和设置输入规则

1.EditText输入限制规则 在xml:EditText 设置属性 android:digits="ABCDE123&*" ABCDE123&*是你的限制规则 例如:android:digits="0123456789abc" 规则是只能输入英文字母(小写)abc和数字 2.EditTex输入的文字为密码形式 (1)在xml中设置 android:password="true" //以"."形式显示文本 (