对于每个应用程序来说,都要有一些属于用户自己的设置,满足不同需求。当我们点击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. <EditTextPreference android:key="user_name"
  9. android:defaultValue="@null"
  10. android:title="@string/preference_username_title"
  11. android:summary="@string/preference_username_summary" />
  12. <ListPreference
  13. android:key="download_format"
  14. android:title="@string/preference_codec_title"
  15. android:summary="@string/preference_codec_summary"
  16. android:entries="@array/stream_codecs"
  17. android:entryValues="@array/stream_codecs_values" />
  18. <ListPreference
  19. android:key="cache_option"
  20. android:title="@string/preference_cache_title"
  21. android:summary="@string/preference_cache_summary"
  22. android:entries="@array/cache_size"
  23. android:entryValues="@array/cache_size_values"/>
  24. <CheckBoxPreference android:key="wifi_only"
  25. android:defaultValue="false"
  26. android:title="@string/preference_wifi_only_title"
  27. android:summary="@string/preference_wifi_only_summary" />
  28. <CheckBoxPreference android:key="roaming_protection"
  29. android:defaultValue="true"
  30. android:title="@string/preference_roaming_summary"
  31. android:summary="@string/preference_roaming_summary" />
  32. </PreferenceCategory>
  33. <PreferenceCategory android:title="@string/preference_3rdparty_title"
  34. android:summary="@string/preference_3rdparty_summary">
  35. <CheckBoxPreference android:defaultValue="false"
  36. android:key="scrobbling_enabled" android:title="@string/scrobbling_enabled"/>
  37. <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>
  38. </PreferenceCategory>
  39. <PreferenceCategory android:title="@string/gestures_preference_title"
  40. android:summary="@string/gestures_preference_summary">
  41. <CheckBoxPreference android:key="gestures"
  42. android:defaultValue="true"
  43. android:title="@string/gestures_support"
  44. android:summary="@string/gestures_support_summary"/>
  45. </PreferenceCategory>
  46. <PreferenceCategory android:title="@string/preference_reset_title">
  47. <Preference android:key="reset_firstrun" android:summary="@string/preference_firstrun_reset_summary" android:title="@string/preference_firstrun_reset_title"></Preference>
  48. </PreferenceCategory>
  49. </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代码  

关于布局文件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. <LinearLayout android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:background="@drawable/gradient_dark_purple"
  7. android:orientation="horizontal" android:gravity="center"
  8. android:minHeight="75dip">
  9. <LinearLayout android:layout_height="wrap_content"
  10. android:minHeight="75dip" android:layout_width="fill_parent"
  11. android:orientation="horizontal" android:gravity="center_vertical"
  12. android:paddingLeft="13dip" android:paddingRight="13dip">
  13. <ImageView android:layout_width="48dip"
  14. android:layout_height="48dip" android:src="@drawable/settings"></ImageView>
  15. <LinearLayout android:layout_height="wrap_content"
  16. android:paddingLeft="13dip" android:orientation="vertical"
  17. android:layout_width="fill_parent">
  18. <TextView android:layout_width="wrap_content"
  19. android:singleLine="true" android:layout_height="wrap_content"
  20. android:textSize="18dip" android:textColor="#ffffff" android:text="@string/settings"></TextView>
  21. <TextView android:layout_width="wrap_content"
  22. android:layout_height="wrap_content" android:textSize="12dip"
  23. android:textColor="#ffffff"></TextView>
  24. <TextView android:id="@+id/ItemsCountTextView"
  25. android:layout_width="wrap_content" android:layout_height="wrap_content"
  26. android:layout_gravity="right" android:textSize="12dip"
  27. android:textColor="#ffffff" android:text=" "></TextView>
  28. </LinearLayout>
  29. </LinearLayout>
  30. </LinearLayout>
  31. <ListView android:layout_width="fill_parent" android:id="@android:id/list"
  32. android:layout_weight="1" android:layout_height="fill_parent">
  33. </ListView>
  34. </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>

PreferenceScreen相关推荐

  1. Android开发--PreferenceActivity中打开子PreferenceScreen黑屏的解决办法

    如果你使用过设置界面,你会很清楚这个主题讲的是什么内容,当你在点击一个设置项之后,跳到另外一个子设置项,你会发现,你看不到任何东西,因为背景是黑色的,所以本文内容讲解的就是这个问题的解决办法,先看运行 ...

  2. (转)android技巧01:Preferencescreen中利用intent跳转activity

    原文连接:http://blog.csdn.net/xianming01/article/details/7543464 设置中的Preferencescreen不仅可以作为设置界面显示,而且还能够启 ...

  3. android preferenceactivity 源码,PreferenceActivity PreferenceScreen (3.0/API 11 以前)

    Android提供了一个XML驱动的框架,用于为应用程序创建系统样式的PreferenceScreen. (3.0/API 11 以前)是这样使用的 看代码: mypreference.xml and ...

  4. android开发中PreferenceScreen的使用注意事项

    今天开发中需要用到android自带的偏好功能PreferenceScreen 在网上教程很多刚开始设置为 <PreferenceScreen xmlns:android="http: ...

  5. android中PreferenceScreen类的用法

    PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 "偏好显示",明白这个意思就好,就是说根据特点灵活的定义显示内 ...

  6. Android PreferenceScreen和CheckBoxPreference的用法

    最近项目里需要修改Android Settings源码,添加自己的功能,需要用到如下两个 PreferenceScreen和CheckBoxPreference 网上的几个博客很有用,分享给大家 ht ...

  7. 针对PreferenceScreen的主题设置

    因项目需要使用PreferenceScreen,类似系统中的设置功能,但是需要设置自定义背景,修改PreferenceScreen中的文本的字体颜色等:可以以下做法: 1:在values目录下新建st ...

  8. Android 首选项框架及PreferenceScreen,PreferenceActivity,PreferenceFragment的用法与分析

    前言 <Android 4 高级编程(第3版)>第7章第5节讲到了首选项框架和PreferenceActivity概述等内容,因为第一次见到PreferenceScreen,还没用过,所以 ...

  9. Preference的使用(2) --- PreferenceCategory PreferenceScreen

    上一节有讲到Preference的基本使用跟API的介绍 ,没有看的话请先阅读 Preference的使用(1) 现在介绍其子类PreferenceCategory  跟 PreferenceScre ...

  10. android 4.2 自定义PreferenceCategory PreferenceScreen Preference等

    需求: 4.2 原生的catagory通常 是一个title, 一条线, 通常是黑色的或白色的线, 现在有一个需求是 改变title的颜色,改变横线颜色的值. 1.  先定义这个category  的 ...

最新文章

  1. Opencv-python 图像处理基础知识
  2. cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)(win7 旗舰版32位)
  3. python 字符串函数 center_Python字符串处理
  4. PostgreSQL 空间处理函数
  5. 【opencv入门篇】 10个程序快速上手opencv【上】
  6. Android 应用快捷方式 ShortcutManager 简单说明
  7. flash 随机函数
  8. win7生成ssh key配置到gitlab
  9. 企业SOA平台 JBoss SOA
  10. ViewPage2+TabLayout小记
  11. 数学建模常用算法案例k—means聚类分析
  12. ckeditor使用----跳坑之旅
  13. 漫谈程序员(十三)健康程序猿系列之男人晚睡的7大危害
  14. 网络广告投放基础,广告
  15. sip php 来电,php - 如何在Twilio上传递原来的来电显示? (Sip域语音URL配置) - SO中文参考 - www.soinside.com...
  16. hadoop 报错 java.io.IOException: There appears to be a gap in the edit log. We expected txid 1, but g
  17. 用canvas绘制微信小程序海报页面并保存相册-适用微信原生
  18. 用批处理命令打开控制面板选项
  19. VJ框架 与 人脸检测/物体检测 详解
  20. vue+iview 兼容IE11浏览器

热门文章

  1. md5和sha256算法的区别,哪个比较安全
  2. 企业全面运营管理沙盘模拟心得_企业运营沙盘模拟心得体会
  3. Ubuntu 15.04 折腾手记
  4. 深信服桌面云的各种密码
  5. 高新技术企业都需要准备哪些资料
  6. ubuntu 虚拟显示器制作
  7. Tssd2019最新版下载地址和更新说明
  8. MOSSE目标跟踪算法步骤
  9. 服务端程序的keeplive
  10. Elsevier旗下期刊利用latex模板撰写论文记录