When to use preference Headers

Source: Internet
Author: User
1. Question: How do I deal with the configuration items that need to be split into screen groups? In the Google development documentation, Android 3.0 is used as the baseline to handle this issue differently:
  • Before Android 3.0: Use the preferencescreen nested method;
  • Android 3.0 and later: the preference headers method is used.

2. The preference. xml file using this method is nested using preferencescreen, for example:
<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 settings of all split screens will be concentrated in an XML file.

3. after preference headersandroid 3.0 is adopted, the traditional preferencescreen Nesting Method is abandoned, but the so-called preference headers method is used. The main points of this method are: all topic settings are listed through the headers XML file layout on the main screen, and the detailed settings of each topic settings are the responsibility of the specific preferencefragment, and their preferencefragment can be like the traditional
Preferenceactivity layout of its own preferencescreen. Headers XML layout styles are shown in the following figure:
<?xml version="1.0" encoding="utf-8"?><preference-headers xmlns:android="http://schemas.android.com/apk/res/android">    

In addition, to display the layout list in headers, The onbuildheaders () callback method must be implemented in the inherited preferenceactivity class:

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

4. advantages of using preference headers I think using this method has the following advantages:

  • This facilitates the adaptation of the same application to devices of different screen sizes. For example, after the preference headers layout is adopted, the system sets the display modes of the application in phone and pad, as shown in:
Phone device split screen layout pad device split screen Layout
  • From the perspective of program design, using headers can reduce the Coupling Degree of preference layout files, and separate the layout originally required to be written in the same XML file for control and maintenance.
  • Other advantages: Hope you can give me some advice.

5. Reference
  • Http://developer.android.com/guide/topics/ui/settings.html
  • Http://developer.android.com/reference/android/preference/PreferenceActivity.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.