標籤:android style blog http color io ar strong 檔案
PreferenceCategory
假設有多個preference,我們希望能在他們組織在一起。有兩種方式,一種就是我們在複合preference中,利用PreferenceScreen進行嵌套,或在同一個PreferenceScreen進行並列放置,這樣的方式之前已經介紹過,不在反覆。還有一種方式是通過PrefrenceCategory進行分類。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學習:Preference(喜好設定)小範例中下載。
相關連結: 我的Android開發相關文章
Pro Android學習筆記(六一):Preferences(5):組織Preference