0
点赞
收藏
分享

微信扫一扫

Pro Android学习笔记(六一):Preferences(5):组织Preference


PreferenceCategory

如果有多个preference,我们希望能在他们组织在一起。有两种方式,一种就是我们在复合preference中,利用PreferenceScreen进行嵌套,或在同一个PreferenceScreen进行并列放置,这种方式之前已经介绍过,不在重复。另一种方式是通过PrefrenceCategory进行分类。xml文件如下:


Pro Android学习笔记(六一):Preferences(5):组织Preference_xml文件

<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>

Child Preference

有时候,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学习笔记(六一):Preferences(5):组织Preference_xml_02

本博文涉及的例子代码,可以在Pro Android学习:Preference(首选项)小例子中下载。


举报

相关推荐

0 条评论