Preferences (4) 3.0 Use Preference headers to group preference items (2015-06-30-23:16)

Source: Internet
Author: User
Tags home screen

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:
  1. <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">
  2. <!--opens a subscreen of settings -
  3. <preferencescreen
  4. android:key="Button_voicemail_category_key"
  5. android:title="@string/voicemail"
  6. android:persistent="false">
  7. <listpreference
  8. android:key="Button_voicemail_provider_key"
  9. android:title="@string/voicemail_provider" ... />
  10. <!--opens another nested subscreen --
  11. <preferencescreen
  12. android:key="Button_voicemail_setting_key"
  13. android:title="@string/voicemail_settings"
  14. android:persistent="false">
  15. ...
  16. </preferencescreen>
  17. <ringtonepreference
  18. android:key="Button_voicemail_ringtone_key"
  19. android:title="@string/voicemail_ringtone_title"
  20. android:ringtonetype="Notification" ... />
  21. ...
  22. </preferencescreen>
  23. ...
  24. </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:
    1. public class  Settingsactivity EXTENDS&NBSP;PREFERENCEACTIVITY&NBSP;{&NBSP;&NBSP;
    2.      @Override   
    3.     public void  Onbuildheaders (List
    4.          loadheadersfromresource (r.xml.preference_headers, target);   
    5. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    6. }  
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:
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
  3. <header
  4. android:fragment="Com.example.prefs.settingsactivity$settingsfragmentone"
  5. android:title="@string/prefs_category_one"
  6. android:summary="@string/prefs_summ_category_one" />
  7. <header
  8. android:fragment="Com.example.prefs.settingsactivity$settingsfragmenttwo"
  9. android:title="@string/prefs_category_two"
  10. android:summary="@string/prefs_summ_category_two" >
  11. <!--Key/value pairs can be included as arguments for the fragment.
  12. <extra android:name="Somekey" android:value="Someheadervalue" />
  13. </Header>
  14. </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)

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.