日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PreferenceScreen

發布時間:2023/12/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PreferenceScreen 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對于每個應用程序來說,都要有一些屬于用戶自己的設置,滿足不同需求。當我們點擊menu時,如下:



?點擊settings時,出現:



?那么這樣的效果是怎么實現的呢?我只是來個簡單介紹,給自己做備忘,也是給大家點思路吧。在android的路上,我們一起努力吧。

這里我們僅說第二個圖片效果的實現,第一個圖片的效果,想必大家都會了,就是使用menu類的幾個方法就可以了。

1.PreferenceScreen 的使用。

首先要定義一下整個布局即使用xml文件夾下的preferences.xml。

代碼如下:

Xml代碼 ?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <PreferenceScreen?xmlns:android="http://schemas.android.com/apk/res/android">??
  • <!--?如果擁有多個首選項,可以構建一個視圖來顯示首選項高級類別。用戶然后就可以深入到每個類別,查看和管理特??
  • 定于該組的首選項。?可以通過兩種方式來實現此目的。可以在根?PreferenceScreen中引入嵌套的?PreferenceScreen?元素,??
  • 或者可以使用?PreferenceCategory?來獲得類似的結果。?-->??
  • ????<PreferenceCategory?android:title="@string/app_name"??
  • ????????android:summary="Application?settings">??
  • ??
  • ????????<EditTextPreference?android:key="user_name"??
  • ????????????android:defaultValue="@null"???
  • ????????????android:title="@string/preference_username_title"??
  • ????????????android:summary="@string/preference_username_summary"?/>??
  • ????????<ListPreference??
  • ????????????????android:key="download_format"??
  • ????????????????android:title="@string/preference_codec_title"??
  • ????????????????android:summary="@string/preference_codec_summary"??
  • ????????????????android:entries="@array/stream_codecs"??
  • ????????????????android:entryValues="@array/stream_codecs_values"?/>??
  • ????????<ListPreference??
  • ????????????????android:key="cache_option"??
  • ????????????????android:title="@string/preference_cache_title"??
  • ????????????????android:summary="@string/preference_cache_summary"??
  • ????????????????android:entries="@array/cache_size"???????????????????
  • ????????????????android:entryValues="@array/cache_size_values"/>??????????????????
  • ????????<CheckBoxPreference?android:key="wifi_only"??
  • ????????????android:defaultValue="false"???
  • ????????????android:title="@string/preference_wifi_only_title"??
  • ????????????android:summary="@string/preference_wifi_only_summary"?/>??
  • ????????<CheckBoxPreference?android:key="roaming_protection"??
  • ????????????android:defaultValue="true"???
  • ????????????android:title="@string/preference_roaming_summary"??
  • ????????????android:summary="@string/preference_roaming_summary"?/>??
  • ??????????
  • ????</PreferenceCategory>??
  • ??
  • ????<PreferenceCategory?android:title="@string/preference_3rdparty_title"??
  • ????????android:summary="@string/preference_3rdparty_summary">??
  • ??
  • ????????<CheckBoxPreference?android:defaultValue="false"???
  • ????????????android:key="scrobbling_enabled"?android:title="@string/scrobbling_enabled"/>??
  • ????????<ListPreference?android:key="scrobbler_app"?android:dependency="scrobbling_enabled"?android:entries="@array/scrobbler_apps"?android:title="@string/scrobbler_app"?android:entryValues="@array/scrobbler_apps_values"?android:summary="@string/scrobbler_app_summary"></ListPreference>??
  • ??????????
  • ????</PreferenceCategory>??
  • ??
  • ????<PreferenceCategory?android:title="@string/gestures_preference_title"??
  • ????????android:summary="@string/gestures_preference_summary">??
  • ????????<CheckBoxPreference?android:key="gestures"??
  • ????????????android:defaultValue="true"??
  • ????????????android:title="@string/gestures_support"??
  • ????????????android:summary="@string/gestures_support_summary"/>??
  • ????</PreferenceCategory>??
  • ????<PreferenceCategory?android:title="@string/preference_reset_title">??
  • ????????<Preference?android:key="reset_firstrun"?android:summary="@string/preference_firstrun_reset_summary"?android:title="@string/preference_firstrun_reset_title"></Preference>??
  • ????</PreferenceCategory>??
  • ??
  • </PreferenceScreen>??
  • ?

    其中:

    特性??????????????????????????????? 說明?
    android:key?????????????????????? 選項的名稱或鍵(比如selected_flight_sort_option)

    android:title?????????????????????? 選項的標題

    android:summary?????????????? 選項的簡短摘要

    android:entries????????????????? 可將選項設置成列表項的文本

    android:entryValues????????? 定義每個列表項的值。注意:每個列表項有一些文本和 一 個 值。 文本由

    entries定義,值由entryValues定義。

    android:dialogTitle???????????? 對話框的標題,在視圖顯示為模態對話框時使用?
    android:defaultValue????????? 項列表中選項的默認值

    ?

    在開發文檔中這樣定義PreferenceScreen,?Represents a top-level Preference that is the root of a Preference hierarchy. 即顯示了首選項組織體系的最頂級。

    ?

    2.PreferenceActivity

    A PreferenceActivity points to an instance of this class to show the preferences. 即我們通過實例化一個繼承PreferenceActivity 的類來展示首選項,代碼如下

    Java代碼 ?
  • public?class?SettingsActivity?extends?PreferenceActivity?{???
  • @Override??
  • ?public?void?onCreate(Bundle?savedInstanceState)???
  • Java代碼 ?
  • {?requestWindowFeature(Window.FEATURE_NO_TITLE);???
  • Java代碼 ?
  • super.onCreate(savedInstanceState);?//添加設置,加入引用???
  • Java代碼 ?
  • addPreferencesFromResource(R.xml.preferences);???
  • setContentView(R.layout.settings);?}??
  • Java代碼 ?
  • }??
  • ?

    ?關于布局文件settings.xml:

    ?

    Xml代碼 ?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"?android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"?android:background="#000000">??
  • ??
  • ????<LinearLayout?android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"?android:background="@drawable/gradient_dark_purple"??
  • ????????android:orientation="horizontal"?android:gravity="center"??
  • ????????android:minHeight="75dip">??
  • ????????<LinearLayout?android:layout_height="wrap_content"??
  • ????????????android:minHeight="75dip"?android:layout_width="fill_parent"??
  • ????????????android:orientation="horizontal"?android:gravity="center_vertical"??
  • ????????????android:paddingLeft="13dip"?android:paddingRight="13dip">??
  • ????????????<ImageView?android:layout_width="48dip"??
  • ????????????????android:layout_height="48dip"?android:src="@drawable/settings"></ImageView>??
  • ????????????<LinearLayout?android:layout_height="wrap_content"??
  • ????????????????android:paddingLeft="13dip"?android:orientation="vertical"??
  • ????????????????android:layout_width="fill_parent">??
  • ????????????????<TextView?android:layout_width="wrap_content"??
  • ????????????????????android:singleLine="true"?android:layout_height="wrap_content"??
  • ????????????????????android:textSize="18dip"?android:textColor="#ffffff"?android:text="@string/settings"></TextView>??
  • ????????????????<TextView?android:layout_width="wrap_content"??
  • ????????????????????android:layout_height="wrap_content"?android:textSize="12dip"??
  • ????????????????????android:textColor="#ffffff"></TextView>??
  • ????????????????<TextView?android:id="@+id/ItemsCountTextView"??
  • ????????????????????android:layout_width="wrap_content"?android:layout_height="wrap_content"??
  • ????????????????????android:layout_gravity="right"?android:textSize="12dip"??
  • ????????????????????android:textColor="#ffffff"?android:text="?"></TextView>??
  • ????????????</LinearLayout>??
  • ????????</LinearLayout>??
  • ????</LinearLayout>??
  • ??
  • ????<ListView?android:layout_width="fill_parent"?android:id="@android:id/list"??
  • ????????android:layout_weight="1"?android:layout_height="fill_parent">??
  • ????</ListView>??
  • </LinearLayout>??
  • ?當我們點擊其中某一項時,如cache option

    效果:

    ?

    ?用到了我們在res下定義的arrays.xml

    其中部分內容如下:

    Xml代碼 ?
  • <resources>?????
  • lt;string-array?name="cache_size">??
  • ?????<item>Off</item>??
  • ?????<item>50?MB</item>??
  • ?????<item>100?MB</item>??
  • ?????<item>250?MB</item>??
  • ?????<item>500?MB</item>??
  • ?</string-array>??????
  • ?<string-array?name="cache_size_values">??
  • ?????<item>0</item>??
  • ?????<item>50</item>??
  • ?????<item>100</item>??
  • ?????<item>250</item>??
  • ?????<item>500</item>??
  • ?</string-array>??
  • ;/resources>??
  • 總結

    以上是生活随笔為你收集整理的PreferenceScreen的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。