Settings源码分析 4.2
1. 拿到一份代码,首先是找到它的入口,一步一步往里看
Settings/AndroidManifest.xml
- <activity android:name="Settings"
- android:label="@string/settings_label_launcher"
- android:taskAffinity="com.android.settings"
- android:configChanges="keyboardHidden|screenSize|mcc|mnc"
- android:launchMode="singleTask">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.LAUNCHER" />
- <action android:name="android.settings.SETTINGS" />
- </intent-filter>
- </activity>
2. 知道主类是Settings
src/com/android/settings/Settings.java
一个Activity根据以往经验,会在onCreate里声明使用的layout文件,如果主layout文件含有fragment, \
则具体的layout应该在实现Fragment类中的onCreateView方法里,不幸的是在Settings里都没有找到,那就是它有自己的机制了,看它的父类吧
- public class Settings extends PreferenceActivity
- implements ButtonBarHandler, OnAccountsUpdateListener {
- ...
- }
3. PreferenceActivity是framework提供的类,去查api吧
1). 概述
这是一个展示preferences结构的类,这个功能经常包含PreferenceFragment这个类。这个类能展示一个或者多个preferences的标题信息, \
每个标题关联一个由PreferenceFragment实现的layout。
它有两种展示方式
I). 在小屏幕上,只展示标题列表,选择一个标题才会显示它关联的界面(PreferenceFragment)
II). 在大屏幕上,同时展示标题列表和内容,内容会根据选择的标题而改变
它的子类应该实现onBuildHeaders(List)这个方法,
2). 所以要重写方法onBuildHeaders来定义自己类的显示效果
-
-
-
- @Override
- public void onBuildHeaders(List<Header> target) {
- loadHeadersFromResource(R.xml.preference_headers, target);
- }
3). 写布局文件
res/xml.preference_headers.xml
- <preference-headers
- xmlns:android="http://schemas./apk/res/android">
-
-
- <header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"
- android:icon="@drawable/ic_settings_applications"
- android:title="Prefs 1"
- android:summary="An example of some preferences." />
-
- <header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
- android:icon="@drawable/ic_settings_display"
- android:title="Prefs 2"
- android:summary="Some other preferences you can see.">
- <!-- Arbitrary key/value pairs can be included with a header as
- arguments to its fragment. -->
- <extra android:name="someKey" android:value="someHeaderValue" />
- </header>
-
- <header android:icon="@drawable/ic_settings_display"
- android:title="Intent"
- android:summary="Launches an Intent.">
- <intent android:action="android.intent.action.VIEW"
- android:data="http://www." />
- </header>
-
- </preference-headers>
在一个header里面可以指定显示的fragment,也可以指定一个Intent,当你用的是phone模式的时候点击这个header就显示fragment或者intent
4). header中指定的fragment是要继承PreferenceFragment,接下来就是PreferenceFragment的使用了
5). 通过addPreferencesFromResource(R.xml.fragmented_preferences);来增加布局文件
-
-
-
-
- public static class Prefs1FragmentInner extends PreferenceFragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
-
- Log.i("args", "Arguments: " + getArguments());
-
-
- addPreferencesFromResource(R.xml.fragmented_preferences_inner);
- }
- }
6). res/xml/fragmented_preferences.xml 这个就是从很早版本就有的Preference了
- <PreferenceScreen
- xmlns:android="http://schemas./apk/res/android">
-
- <PreferenceCategory
- android:title="@string/inline_preferences">
-
-
- <CheckBoxPreference
- android:key="checkbox_preference"
- android:title="@string/title_checkbox_preference"
- android:summary="@string/summary_checkbox_preference" />
- </PreferenceCategory>
-
- <PreferenceCategory
- android:title="@string/dialog_based_preferences">
-
- <EditTextPreference
- android:key="edittext_preference"
- android:title="@string/title_edittext_preference"
- android:summary="@string/summary_edittext_preference"
- android:dialogTitle="@string/dialog_title_edittext_preference" />
-
- <ListPreference
- android:key="list_preference"
- android:title="@string/title_list_preference"
- android:summary="@string/summary_list_preference"
- android:entries="@array/entries_list_preference"
- android:entryValues="@array/entryvalues_list_preference"
- android:dialogTitle="@string/dialog_title_list_preference" />
- </PreferenceCategory>
-
- <PreferenceCategory
- android:title="@string/launch_preferences">
- <!-- This PreferenceScreen tag sends the user to a new fragment of
- preferences. If running in a large screen, they can be embedded
- inside of the overall preferences UI. -->
- <PreferenceScreen
- android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
- android:title="@string/title_fragment_preference"
- android:summary="@string/summary_fragment_preference">
-
- <extra android:name="someKey" android:value="somePrefValue" />
- </PreferenceScreen>
-
- <!-- This PreferenceScreen tag sends the user to a completely different
- activity, switching out of the current preferences UI. -->
- <PreferenceScreen
- android:title="@string/title_intent_preference"
- android:summary="@string/summary_intent_preference">
- <intent android:action="android.intent.action.VIEW"
- android:data="http://www." />
- </PreferenceScreen>
- </PreferenceCategory>
- <PreferenceCategory
- android:title="@string/preference_attributes">
- <CheckBoxPreference
- android:key="parent_checkbox_preference"
- android:title="@string/title_parent_preference"
- android:summary="@string/summary_parent_preference" />
-
-
- <CheckBoxPreference
- android:key="child_checkbox_preference"
- android:dependency="parent_checkbox_preference"
- android:layout="?android:attr/preferenceLayoutChild"
- android:title="@string/title_child_preference"
- android:summary="@string/summary_child_preference" />
- </PreferenceCategory>
- </PreferenceScreen>
4. 所以总结下来,Settings需要了解PreferenceActivity,PreferenceFragment 这两个类,preference-headers,PreferenceScreen这2中xml文件它的结构就清楚了,其他的就是相互调用了,和之前版本一样
|