Preference的API中定义了如下属性,我们通过实例一一分解它们的含义和用处。本例借助一个CheckBoxPreference分析,而且只分析Preference里的各项属性,对于子类的属性我们这里不作介绍。
MainActivity.java:
package com.sean.preferenceattributestest;
import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.attributes_preference);
}
}
}
attributes_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="@string/PreferenceCategory_summary"
android:title="@string/PreferenceCategory_title" >
<CheckBoxPreference
android:icon="@drawable/ic_launcher"
android:key="CheckBoxPreference_key1"
android:summaryOff="@string/CheckBoxPreference_summaryOff"
android:summaryOn="@string/CheckBoxPreference_summaryOn"
android:title="@string/CheckBoxPreference_title" />
</PreferenceCategory>
<PreferenceCategory
android:summary="@string/PreferenceCategory_summary"
android:title="@string/PreferenceCategory_title" >
<CheckBoxPreference
android:icon="@drawable/ic_launcher"
android:key="CheckBoxPreference_key2"
android:summaryOff="@string/CheckBoxPreference_summaryOff"
android:summaryOn="@string/CheckBoxPreference_summaryOn"
android:title="@string/CheckBoxPreference_title" />
</PreferenceCategory>
</PreferenceScreen>
这个基础XML分析了五项基本属性,表现出来如下左图:
接下来添加各项属性一一分析:
android:enabled第一组为true,第二组为false,如右图所示,第二组处于阴影状态,不能操作,第一组同缺省状态下相同
android:selectable第一组为true,第二组为false,如左图所示,但是点击第二组无任何响应,第一组同缺省状态相同
android:dependency第二组添加android:dependency="CheckBoxPreference_key1",内容为第一组的key值,现象为当第一组checked时,第二组可正常操作;第一组unChecked时,第二组处阴影状态,不可操作。
下面我们来分析android:layout=“checkboxpreferencelayout”和android:widgetLayout=“checkboxlayout”。这两个属性可以重新定义一个Preference的样式,比如说我们重新定义第而组CheckBoxPreference的XML文件和效果如下:此处系统CheckBox的方框由一颗足球代替,点击足球仍然有summaryOn和summaryOff的变化,即效果与原先相同
checkboxpreferencelayout.xml如下:对比这个layout和第一组CheckBoxPreference的视图样式,我们发现它的定义要遵守一定的规则,才能定义出我们想要的视图。不管我们如何描述,最后呈现出来的这个CheckBoxLayout的视图由这个android:layout得到;对于summary,title,icon这三项,我们必须把它们的id定义为系统id,这样才能把<CheckBoxPreference>中定义的这些内容填充到这个layout中来,比如我们在layout中没有响应的icon的id,那么<CheckBoxPreference>中的icon图标失效。其次一点对于控件(本例中的CheckBox)它的id需要申明为widget_frame,然后才可以和android:widgetLayout对应起来。当然我们也可以直接在layout中定义CheckBox,这样我们就不需要使用android:widgetLayout了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/calendar"
android:gravity="center_vertical"
android:maxHeight="40dp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6.0dip"
android:layout_marginLeft="15.0dip"
android:layout_marginRight="6.0dip"
android:layout_marginTop="6.0dip"
android:layout_weight="1.0" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="18.0sp" />
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="12.0sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
checkboxlayout.xml如下:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/checkbox"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="4.0dip"
android:button="@drawable/btn_code_lock_default_holo"
android:clickable="false"
android:focusable="false" />
从上我们可以总结出android:layout和android:widgetLayout的作用是自定义Preference的视图样式。
现在还有以下几个属性
android:defaultValue——Preference会保存一个value值,这个属性是设置这个缺省值
android:fragment——应用在PreferenceActivity中时,当用户点击这个Preference时,启动一个新的fragment。此例是一个PreferenceFragment,作实验无效果
android:order——The order for the Preference (lower values are to be ordered first). 没实验出来,不解何意。
android:persistent——是否把值保存到SharedPreference中
android:shouldDisableView——Whether the view of this Preference should be disabled when this Preference is disabled. (disable在此处不懂何意)