先推荐两篇总结得比较好的文章:
Android应用Preference相关及源码浅析(Preference组件家族篇)
本人一篇关于自定义preference的文章
http://blog.youkuaiyun.com/b1480521874/article/details/77603364
先看详细的还是看上面两个链接吧。下面我只是对上面链接的两篇文章做一个概括。
Preference及其体系的用处
1.preference
的实现解析xml中的属性,作为之后创建对应View的一些参数。就是每个preference子类型,对应不同的一个layout,
类似menu的解析和view的生成过程一样
onCreateView():用于生成一个Preference的ViewGroup,使用的layout是com.android.internal.R.layout.preference
getView()->onBindView():将属性填充入onCreateView生成的ViewGroup中。并返回对应的ViewGroup
2.preferenceFragment
有两种方法去绑定preference资源:
* addPreferencesFromResource
* addPreferencesFromIntent
* 这两个方法需要在super.oncreate()后再调用,否则会因为PreferenceManager没有创建而报错。
设定一个根PreferenceScreen,PreferenceScreen是preference的容器。
通过getListVIew()获得那个装入所有Preference的ViewGroup,每个Preference对应一个ListView的Item
preferenceGroupAdapter作为ListView的适配器,最终通过Preference#getView()获得Preference对应的
View,该ListView就是R.id.list,通过PreferenceFragment#getListView()获得。
下是preferenceFragment的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/transparent"
android:layout_removeBorders="true">
<ListView android:id="@android:id/list"
style="?attr/preferenceFragmentListStyle"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:paddingTop="0dip"
android:paddingBottom="@dimen/preference_fragment_padding_bottom"
android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
android:clipToPadding="false"
android:drawSelectorOnTop="false"
android:cacheColorHint="@android:color/transparent"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/preference_fragment_padding_side"
android:gravity="center"
android:visibility="gone" />
<RelativeLayout android:id="@+id/button_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="0"
android:visibility="gone">
<Button android:id="@+id/back_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentStart="true"
android:text="@string/back_button_label"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<Button android:id="@+id/skip_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/skip_button_label"
android:visibility="gone"
/>
<Button android:id="@+id/next_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/next_button_label"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
在fragment中,与fragment绑定的view是通过调用Fragment#onCreateView()获取的。那么
preferenceFragment的view又是从哪里获取的呢?
答案:也是从oncreateview处获取的。就是Fragment的根View还是从oncreatView那里获取,
在绑定后,preferencemanager会将preference解析好生成的view填充到fragment的rootview
的ListView中。所以oncreateview的return的view对应的布局文件要规范,及其ListView的id为
@android:id/list,和ListActvity的一样。如果不规范的话,通过addPreferencefromSource加
进来的view就无法真正加进来了
3.PreferenceActivity extends ListActivity
不推荐使用其addPreferencesFromResource,因为这样跟preferencefragment没多大区别。而是推荐使用loadHeadersFromResource代替。该Activity的基础布局是preference_list_content.
可见水平LinearLayout中,包含两个布局headers和prefs_frame.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
<LinearLayout
style="?attr/preferenceHeaderPanelStyle"
android:id="@+id/headers"
android:orientation="vertical"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="@integer/preferences_left_pane_weight">
<ListView android:id="@android:id/list"
style="?attr/preferenceListStyle"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:clipToPadding="false"
android:drawSelectorOnTop="false"
android:cacheColorHint="@android:color/transparent"
android:listPreferredItemHeight="48dp"
android:scrollbarAlwaysDrawVerticalTrack="true" />
<FrameLayout android:id="@+id/list_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
<LinearLayout
android:id="@+id/prefs_frame"
style="?attr/preferencePanelStyle"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="@integer/preferences_right_pane_weight"
android:orientation="vertical"
android:visibility="gone" >
<!-- Breadcrumb inserted here, in certain screen sizes. In others, it will be an
empty layout or just padding, and PreferenceActivity will put the breadcrumbs in
the action bar. -->
<include layout="@layout/breadcrumbs_in_fragment" />
<android.preference.PreferenceFrameLayout android:id="@+id/prefs"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout android:id="@+id/button_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="0"
android:visibility="gone">
<Button android:id="@+id/back_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentStart="true"
android:text="@string/back_button_label"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<Button android:id="@+id/skip_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/skip_button_label"
android:visibility="gone"
/>
<Button android:id="@+id/next_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/next_button_label"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
使用loadHeadersFromResources的文章链接如下:
http://www.cnblogs.com/gelandesprung/p/4231980.html
使用loadHeadersFromResources的标准布局是左边是headers列表,右边是选中header对应的fragment。
loadHeadersFromResources在onBuildHeaders(List<header>)中加入,以加载xml.
本文概述了Android中Preference的使用,包括Preference的解析、PreferenceFragment的绑定方法以及PreferenceActivity的扩展。重点讨论了Preference如何根据XML属性生成视图,PreferenceFragment的addPreferencesFromResource和addPreferencesFromIntent方法的调用时机,以及PreferenceScreen作为容器的角色。同时,指出了在自定义布局时应注意的ListView id规范问题。
213

被折叠的 条评论
为什么被折叠?



