打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
Settings源码分析
lifei_szdz
>《android_setting》
2013.09.09
关注
Settings源码分析 4.2
1. 拿到一份代码,首先是找到它的入口,一步一步往里看
Settings/AndroidManifest.xml
[html]
view plain
copy
?
<
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里都没有找到,那就是它有自己的机制了,看它的父类吧
[java]
view plain
copy
?
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来定义自己类的显示效果
[java]
view plain
copy
?
/**
* Populate the activity with the top-level headers.
*/
@Override
public
void
onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
3). 写布局文件
res/xml.preference_headers.xml
[html]
view plain
copy
?
<
preference-headers
xmlns:android
=
"http://schemas.android.com/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.android.com"
/>
</
header
>
</
preference-headers
>
在一个header里面可以指定显示的fragment,也可以指定一个Intent,当你用的是phone模式的时候点击这个header就显示fragment或者intent
4). header中指定的fragment是要继承PreferenceFragment,接下来就是PreferenceFragment的使用了
5). 通过addPreferencesFromResource(R.xml.fragmented_preferences);来增加布局文件
[java]
view plain
copy
?
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public
static
class
Prefs1FragmentInner
extends
PreferenceFragment {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
// Can retrieve arguments from preference XML.
Log.i(
"args"
,
"Arguments: "
+ getArguments());
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences_inner);
}
}
6). res/xml/fragmented_preferences.xml 这个就是从很早版本就有的Preference了
[html]
view plain
copy
?
<
PreferenceScreen
xmlns:android
=
"http://schemas.android.com/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"
>
<!-- Arbitrary key/value pairs can be included for fragment arguments -->
<
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.android.com"
/>
</
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"
/>
<!-- The visual style of a child is defined by this styled theme attribute. -->
<
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文件它的结构就清楚了,其他的就是相互调用了,和之前版本一样
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
Android实现全屏图片的欢迎界面
Android Launcher manifest解析
浅谈unity接入华为SDK流程
androidactivity详解一:概述
android:label说明
Getting Started with Android Studio
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×