PreferenceScreen

来源:互联网 发布:恢复数据多少钱 编辑:程序博客网 时间:2024/04/25 05:53
对于每个应用程序来说,都要有一些属于用户自己的设置,满足不同需求。当我们点击menu时,如下:



 点击settings时,出现:



 那么这样的效果是怎么实现的呢?我只是来个简单介绍,给自己做备忘,也是给大家点思路吧。在android的路上,我们一起努力吧。

这里我们仅说第二个图片效果的实现,第一个图片的效果,想必大家都会了,就是使用menu类的几个方法就可以了。

1.PreferenceScreen 的使用。

首先要定义一下整个布局即使用xml文件夹下的preferences.xml。

代码如下:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <!-- 如果拥有多个首选项,可以构建一个视图来显示首选项高级类别。用户然后就可以深入到每个类别,查看和管理特  
  4. 定于该组的首选项。 可以通过两种方式来实现此目的。可以在根 PreferenceScreen中引入嵌套的 PreferenceScreen 元素,  
  5. 或者可以使用 PreferenceCategory 来获得类似的结果。 -->  
  6.     <PreferenceCategory android:title="@string/app_name"  
  7.         android:summary="Application settings">  
  8.   
  9.         <EditTextPreference android:key="user_name"  
  10.             android:defaultValue="@null"   
  11.             android:title="@string/preference_username_title"  
  12.             android:summary="@string/preference_username_summary" />  
  13.         <ListPreference  
  14.                 android:key="download_format"  
  15.                 android:title="@string/preference_codec_title"  
  16.                 android:summary="@string/preference_codec_summary"  
  17.                 android:entries="@array/stream_codecs"  
  18.                 android:entryValues="@array/stream_codecs_values" />  
  19.         <ListPreference  
  20.                 android:key="cache_option"  
  21.                 android:title="@string/preference_cache_title"  
  22.                 android:summary="@string/preference_cache_summary"  
  23.                 android:entries="@array/cache_size"                   
  24.                 android:entryValues="@array/cache_size_values"/>                  
  25.         <CheckBoxPreference android:key="wifi_only"  
  26.             android:defaultValue="false"   
  27.             android:title="@string/preference_wifi_only_title"  
  28.             android:summary="@string/preference_wifi_only_summary" />  
  29.         <CheckBoxPreference android:key="roaming_protection"  
  30.             android:defaultValue="true"   
  31.             android:title="@string/preference_roaming_summary"  
  32.             android:summary="@string/preference_roaming_summary" />  
  33.           
  34.     </PreferenceCategory>  
  35.   
  36.     <PreferenceCategory android:title="@string/preference_3rdparty_title"  
  37.         android:summary="@string/preference_3rdparty_summary">  
  38.   
  39.         <CheckBoxPreference android:defaultValue="false"   
  40.             android:key="scrobbling_enabled" android:title="@string/scrobbling_enabled"/>  
  41.         <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>  
  42.           
  43.     </PreferenceCategory>  
  44.   
  45.     <PreferenceCategory android:title="@string/gestures_preference_title"  
  46.         android:summary="@string/gestures_preference_summary">  
  47.         <CheckBoxPreference android:key="gestures"  
  48.             android:defaultValue="true"  
  49.             android:title="@string/gestures_support"  
  50.             android:summary="@string/gestures_support_summary"/>  
  51.     </PreferenceCategory>  
  52.     <PreferenceCategory android:title="@string/preference_reset_title">  
  53.         <Preference android:key="reset_firstrun" android:summary="@string/preference_firstrun_reset_summary" android:title="@string/preference_firstrun_reset_title"></Preference>  
  54.     </PreferenceCategory>  
  55.   
  56. </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代码  收藏代码
  1. public class SettingsActivity extends PreferenceActivity {   
  2. @Override  
  3.  public void onCreate(Bundle savedInstanceState)   
Java代码  收藏代码
  1. { requestWindowFeature(Window.FEATURE_NO_TITLE);   
Java代码  收藏代码
  1. super.onCreate(savedInstanceState); //添加设置,加入引用   
Java代码  收藏代码
  1. addPreferencesFromResource(R.xml.preferences);   
  2. setContentView(R.layout.settings); }  
Java代码  收藏代码
  1. }  

 

 关于布局文件settings.xml:

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:background="#000000">  
  5.   
  6.     <LinearLayout android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content" android:background="@drawable/gradient_dark_purple"  
  8.         android:orientation="horizontal" android:gravity="center"  
  9.         android:minHeight="75dip">  
  10.         <LinearLayout android:layout_height="wrap_content"  
  11.             android:minHeight="75dip" android:layout_width="fill_parent"  
  12.             android:orientation="horizontal" android:gravity="center_vertical"  
  13.             android:paddingLeft="13dip" android:paddingRight="13dip">  
  14.             <ImageView android:layout_width="48dip"  
  15.                 android:layout_height="48dip" android:src="@drawable/settings"></ImageView>  
  16.             <LinearLayout android:layout_height="wrap_content"  
  17.                 android:paddingLeft="13dip" android:orientation="vertical"  
  18.                 android:layout_width="fill_parent">  
  19.                 <TextView android:layout_width="wrap_content"  
  20.                     android:singleLine="true" android:layout_height="wrap_content"  
  21.                     android:textSize="18dip" android:textColor="#ffffff" android:text="@string/settings"></TextView>  
  22.                 <TextView android:layout_width="wrap_content"  
  23.                     android:layout_height="wrap_content" android:textSize="12dip"  
  24.                     android:textColor="#ffffff"></TextView>  
  25.                 <TextView android:id="@+id/ItemsCountTextView"  
  26.                     android:layout_width="wrap_content" android:layout_height="wrap_content"  
  27.                     android:layout_gravity="right" android:textSize="12dip"  
  28.                     android:textColor="#ffffff" android:text=" "></TextView>  
  29.             </LinearLayout>  
  30.         </LinearLayout>  
  31.     </LinearLayout>  
  32.   
  33.     <ListView android:layout_width="fill_parent" android:id="@android:id/list"  
  34.         android:layout_weight="1" android:layout_height="fill_parent">  
  35.     </ListView>  
  36. </LinearLayout>  

 当我们点击其中某一项时,如cache option

效果:

 

 用到了我们在res下定义的arrays.xml

其中部分内容如下:

Xml代码  收藏代码
  1. <resources>     
  2. lt;string-array name="cache_size">  
  3.      <item>Off</item>  
  4.      <item>50 MB</item>  
  5.      <item>100 MB</item>  
  6.      <item>250 MB</item>  
  7.      <item>500 MB</item>  
  8.  </string-array>      
  9.  <string-array name="cache_size_values">  
  10.      <item>0</item>  
  11.      <item>50</item>  
  12.      <item>100</item>  
  13.      <item>250</item>  
  14.      <item>500</item>  
  15.  </string-array>  
  16. ;/resources>  

原创粉丝点击