分享

Settings源码分析

 lifei_szdz 2013-09-09
Settings源码分析 4.2
1. 拿到一份代码,首先是找到它的入口,一步一步往里看
Settings/AndroidManifest.xml
  1. <activity android:name="Settings"   
  2.                 android:label="@string/settings_label_launcher"  
  3.                 android:taskAffinity="com.android.settings"  
  4.                 android:configChanges="keyboardHidden|screenSize|mcc|mnc"  
  5.                 android:launchMode="singleTask">  
  6.             <intent-filter>  
  7.                 <action android:name="android.intent.action.MAIN" />  
  8.                 <category android:name="android.intent.category.DEFAULT" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.                 <action android:name="android.settings.SETTINGS" />  
  11.             </intent-filter>  
  12.         </activity>  

2. 知道主类是Settings
src/com/android/settings/Settings.java
一个Activity根据以往经验,会在onCreate里声明使用的layout文件,如果主layout文件含有fragment,  \
则具体的layout应该在实现Fragment类中的onCreateView方法里,不幸的是在Settings里都没有找到,那就是它有自己的机制了,看它的父类吧
  1. public class Settings extends PreferenceActivity  
  2.         implements ButtonBarHandler, OnAccountsUpdateListener {  
  3.         ...  
  4.     }  
    
3. PreferenceActivity是framework提供的类,去查api吧
1). 概述
这是一个展示preferences结构的类,这个功能经常包含PreferenceFragment这个类。这个类能展示一个或者多个preferences的标题信息, \
每个标题关联一个由PreferenceFragment实现的layout。
它有两种展示方式
I). 在小屏幕上,只展示标题列表,选择一个标题才会显示它关联的界面(PreferenceFragment)
II). 在大屏幕上,同时展示标题列表和内容,内容会根据选择的标题而改变
它的子类应该实现onBuildHeaders(List)这个方法,
2). 所以要重写方法onBuildHeaders来定义自己类的显示效果
  1. /** 
  2.      * Populate the activity with the top-level headers. 
  3.      */  
  4.     @Override  
  5.     public void onBuildHeaders(List<Header> target) {  
  6.         loadHeadersFromResource(R.xml.preference_headers, target);  
  7.     }  
3). 写布局文件
    res/xml.preference_headers.xml
  1. <preference-headers  
  2.         xmlns:android="http://schemas./apk/res/android">  
  3.   
  4.   
  5.     <header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"  
  6.             android:icon="@drawable/ic_settings_applications"  
  7.             android:title="Prefs 1"  
  8.             android:summary="An example of some preferences." />  
  9.   
  10.     <header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"  
  11.             android:icon="@drawable/ic_settings_display"  
  12.             android:title="Prefs 2"  
  13.             android:summary="Some other preferences you can see.">  
  14.         <!-- Arbitrary key/value pairs can be included with a header as  
  15.              arguments to its fragment. -->  
  16.         <extra android:name="someKey" android:value="someHeaderValue" />  
  17.     </header>  
  18.   
  19.     <header android:icon="@drawable/ic_settings_display"  
  20.             android:title="Intent"  
  21.             android:summary="Launches an Intent.">  
  22.         <intent android:action="android.intent.action.VIEW"  
  23.                 android:data="http://www." />  
  24.     </header>  
  25.   
  26.     </preference-headers>  
在一个header里面可以指定显示的fragment,也可以指定一个Intent,当你用的是phone模式的时候点击这个header就显示fragment或者intent

4). header中指定的fragment是要继承PreferenceFragment,接下来就是PreferenceFragment的使用了
5). 通过addPreferencesFromResource(R.xml.fragmented_preferences);来增加布局文件
  1. /** 
  2.      * This fragment contains a second-level set of preference that you 
  3.      * can get to by tapping an item in the first preferences fragment. 
  4.      */  
  5.     public static class Prefs1FragmentInner extends PreferenceFragment {  
  6.         @Override  
  7.         public void onCreate(Bundle savedInstanceState) {  
  8.             super.onCreate(savedInstanceState);  
  9.   
  10.             // Can retrieve arguments from preference XML.  
  11.             Log.i("args""Arguments: " + getArguments());  
  12.   
  13.             // Load the preferences from an XML resource  
  14.             addPreferencesFromResource(R.xml.fragmented_preferences_inner);  
  15.         }  
  16.     }  

6). res/xml/fragmented_preferences.xml 这个就是从很早版本就有的Preference了
  1. <PreferenceScreen  
  2.         xmlns:android="http://schemas./apk/res/android">  
  3.   
  4.     <PreferenceCategory  
  5.             android:title="@string/inline_preferences">  
  6.   
  7.   
  8.         <CheckBoxPreference  
  9.                 android:key="checkbox_preference"  
  10.                 android:title="@string/title_checkbox_preference"  
  11.                 android:summary="@string/summary_checkbox_preference" />  
  12.     </PreferenceCategory>  
  13.   
  14.     <PreferenceCategory  
  15.             android:title="@string/dialog_based_preferences">  
  16.   
  17.         <EditTextPreference  
  18.                 android:key="edittext_preference"  
  19.                 android:title="@string/title_edittext_preference"  
  20.                 android:summary="@string/summary_edittext_preference"  
  21.                 android:dialogTitle="@string/dialog_title_edittext_preference" />  
  22.   
  23.         <ListPreference  
  24.                 android:key="list_preference"  
  25.                 android:title="@string/title_list_preference"  
  26.                 android:summary="@string/summary_list_preference"  
  27.                 android:entries="@array/entries_list_preference"  
  28.                 android:entryValues="@array/entryvalues_list_preference"  
  29.                 android:dialogTitle="@string/dialog_title_list_preference" />  
  30.     </PreferenceCategory>  
  31.   
  32.     <PreferenceCategory  
  33.             android:title="@string/launch_preferences">  
  34.         <!-- This PreferenceScreen tag sends the user to a new fragment of  
  35.              preferences.  If running in a large screen, they can be embedded  
  36.              inside of the overall preferences UI. -->  
  37.         <PreferenceScreen  
  38.                 android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"  
  39.                 android:title="@string/title_fragment_preference"  
  40.                 android:summary="@string/summary_fragment_preference">  
  41.             <!-- Arbitrary key/value pairs can be included for fragment arguments -->  
  42.             <extra android:name="someKey" android:value="somePrefValue" />  
  43.         </PreferenceScreen>  
  44.   
  45.         <!-- This PreferenceScreen tag sends the user to a completely different  
  46.              activity, switching out of the current preferences UI. -->  
  47.         <PreferenceScreen  
  48.                 android:title="@string/title_intent_preference"  
  49.                 android:summary="@string/summary_intent_preference">  
  50.             <intent android:action="android.intent.action.VIEW"  
  51.                     android:data="http://www." />  
  52.         </PreferenceScreen>  
  53.     </PreferenceCategory>  
  54.     <PreferenceCategory  
  55.             android:title="@string/preference_attributes">  
  56.         <CheckBoxPreference  
  57.                 android:key="parent_checkbox_preference"  
  58.                 android:title="@string/title_parent_preference"  
  59.                 android:summary="@string/summary_parent_preference" />  
  60.   
  61.         <!-- The visual style of a child is defined by this styled theme attribute. -->  
  62.         <CheckBoxPreference  
  63.                 android:key="child_checkbox_preference"  
  64.                 android:dependency="parent_checkbox_preference"  
  65.                 android:layout="?android:attr/preferenceLayoutChild"  
  66.                 android:title="@string/title_child_preference"  
  67.                 android:summary="@string/summary_child_preference" />  
  68.     </PreferenceCategory>  
  69. </PreferenceScreen>  
4. 所以总结下来,Settings需要了解PreferenceActivity,PreferenceFragment 这两个类,preference-headers,PreferenceScreen这2中xml文件它的结构就清楚了,其他的就是相互调用了,和之前版本一样

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多