Preference、PreferenceActivity,PreferenceFragment

本文概述了Android中Preference的使用,包括Preference的解析、PreferenceFragment的绑定方法以及PreferenceActivity的扩展。重点讨论了Preference如何根据XML属性生成视图,PreferenceFragment的addPreferencesFromResource和addPreferencesFromIntent方法的调用时机,以及PreferenceScreen作为容器的角色。同时,指出了在自定义布局时应注意的ListView id规范问题。

先推荐两篇总结得比较好的文章:

Android中Preference的使用以及监听事件分析

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.






                
package com.brome.usbnet; import android.annotation.TargetApi;Cannot resolve method 'bindPreferenceSummaryToValue' in 'SettingsWapiActivity' import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.LinearLayout; public class SettingsWapiActivity extends PreferenceActivity { private static final boolean ALWAYS_SIMPLE_PREFS = false; private static final String TAG = "SettingsWapiActivity"; protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setupSimplePreferencesScreen(); } private void setupSimplePreferencesScreen() { addPreferencesFromResource(R.xml.pref_wapi_wireless); addPreferencesFromResource(R.xml.pref_wapi_button); bindPreferenceSummaryToValue(findPreference("pref_wireless_encryption_method_ap1")); bindPreferenceSummaryToValue(findPreference("pref_wireless_ssid_ap1")); bindPreferenceSummaryToValue(findPreference("pref_wireless_key_ap1")); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class GeneralPreferenceFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref_wapi_wireless); } } }
最新发布
06-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值