Transferred from: http://blog.csdn.net/ixiaobu/article/details/8609935
1. Question: How do I handle setting items that require split-screen grouping? This issue is handled differently in the Google development documentation using the Android 3.0 version as a baseline:
- Before Android 3.0: Using Preferencescreen nesting method;
- Android 3.0 and later: Adopt preference headers method.
2. Using Preferencescreen nested Preference.xml files with this method are as follows:
- <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">
- <!--opens a subscreen of settings -
- <preferencescreen
- android:key="Button_voicemail_category_key"
- android:title="@string/voicemail"
- android:persistent="false">
- <listpreference
- android:key="Button_voicemail_provider_key"
- android:title="@string/voicemail_provider" ... />
- <!--opens another nested subscreen --
- <preferencescreen
- android:key="Button_voicemail_setting_key"
- android:title="@string/voicemail_settings"
- android:persistent="false">
- ...
- </preferencescreen>
- <ringtonepreference
- android:key="Button_voicemail_ringtone_key"
- android:title="@string/voicemail_ringtone_title"
- android:ringtonetype="Notification" ... />
- ...
- </preferencescreen>
- ...
- </preferencescreen>
In this way, the setup instructions for all the split screens will be concentrated in an XML file. In addition, in order to be able to display a list of layouts in headers, the Onbuildheaders () callback method needs to be implemented in the inherited Preferenceactivity class:
- public class Settingsactivity EXTENDS&NBSP;PREFERENCEACTIVITY&NBSP;{&NBSP;&NBSP;
- @Override
- public void Onbuildheaders (List
- loadheadersfromresource (r.xml.preference_headers, target);
- &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
- }
3. After adopting the Preference Headersandroid 3.0, the traditional Preferencescreen nesting method is discarded, but the so-called Preference Headers method is adopted, the main point of which is: in the home screen through the Headers The XML file layout lists all the theme settings, and the detailed settings for each theme are assigned by the respective preferencefragment, and the respective preferencefragment can be as traditional preferenceactivity The same preferencescreen as the layout itself. Headers XML layout styles such as:
- <? 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>
4. Benefits of using Preference Headers I think this approach has the following advantages:
- It is convenient for the same application to fit in different screen size devices, for example, after preference headers layout, the system settings are displayed in the phone and pad as shown in the following ways:
Phone device split screen layout pad device split screen layout
- From the point of view of program design, adopting headers can reduce the coupling degree of preference layout file, separate the layout which originally needs to be written in the same XML file, and control and maintain each other independently.
- Other advantages: Hope Master enlighten.
5. Reference
- Http://developer.android.com/guide/topics/ui/settings.html
- Http://developer.android.com/reference/android/preference/PreferenceActivity.html
Preferences (4) 3.0 Use Preference headers to group preference items (2015-06-30-23:16)