标签:android style blog http color io ar strong 文件
假设有多个preference,我们希望能在他们组织在一起。有两种方式,一种就是我们在复合preference中,利用PreferenceScreen进行嵌套,或在同一个PreferenceScreen进行并列放置,这样的方式之前已经介绍过,不在反复。还有一种方式是通过PrefrenceCategory进行分类。xml文件例如以下:
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:key="meats_category" 
        android:title="Meats" 
        android:summary="Preferences related to meats"> 
        <CheckBoxPreference android:key="fish_selection_pref"
            android:title="Fish" 
            android:summary="Fish is Healthy"/> 
        <CheckBoxPreference  … />  
        <CheckBoxPreference … /> 
    </PreferenceCategory> 
    <PreferenceCategory android:key="vegitable_category" 
        android:title="Vegetables" 
        android:summary="Preference related to Vegetables"> 
        <CheckBoxPreference android:key="tomato_selection_pref"
            android:title="Tomato" 
            android:summary="It‘s actually a fruit."/> 
        <CheckBoxPreference …/> 
    </PreferenceCategory> 
</PreferenceScreen> 
有时候,preference相互间有依存性,如仅仅有A为true时,B才有效。以下的样例是,仅仅有选择了告警,怎样告警才会有效,否则会变灰。xml文件例如以下:
<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/child_preferences"> 
    <PreferenceCategory android:title="Alerts">
        <CheckBoxPreference android:key="alter_email" 
            android:title="Send email?"/> 
        <EditTextPreference android:key="alter_email_address" 
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="Email Address" 
            android:dependency="alter_email"/> <!-- 设置关联,仅仅有key为alter_email的为true,才enable本perf,同一时候通过android:attr/设置layout的格式 -->
    </PreferenceCategory> 
</PreferenceScreen>

本博文涉及的样例代码,能够在Pro Android学习:Preference(首选项)小样例中下载。
相关链接: 我的Android开发相关文章
Pro Android学习笔记(六一):Preferences(5):组织Preference
标签:android style blog http color io ar strong 文件
原文地址:http://www.cnblogs.com/hrhguanli/p/4020409.html