Pro Android學習筆記(六一):Preferences(5):組織Preference

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.