PreferenceScreen
对于每个应用程序来说,都要有一些属于用户自己的设置,满足不同需求。当我们点击menu时,如下:
点击settings时,出现:
那么这样的效果是怎么实现的呢?我只是来个简单介绍,给自己做备忘,也是给大家点思路吧。在android的路上,我们一起努力吧。
这里我们仅说第二个图片效果的实现,第一个图片的效果,想必大家都会了,就是使用menu类的几个方法就可以了。
1.PreferenceScreen 的使用。
首先要定义一下整个布局即使用xml文件夹下的preferences.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 的类来展示首选项,代码如下
- public class SettingsActivity extends PreferenceActivity {
- @Override
- public void onCreate(Bundle savedInstanceState)
- { requestWindowFeature(Window.FEATURE_NO_TITLE);
- super.onCreate(savedInstanceState); //添加设置,加入引用
- addPreferencesFromResource(R.xml.preferences);
- setContentView(R.layout.settings); }
- }
关于布局文件settings.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
其中部分内容如下:
- <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相关推荐
- Android开发--PreferenceActivity中打开子PreferenceScreen黑屏的解决办法
如果你使用过设置界面,你会很清楚这个主题讲的是什么内容,当你在点击一个设置项之后,跳到另外一个子设置项,你会发现,你看不到任何东西,因为背景是黑色的,所以本文内容讲解的就是这个问题的解决办法,先看运行 ...
- (转)android技巧01:Preferencescreen中利用intent跳转activity
原文连接:http://blog.csdn.net/xianming01/article/details/7543464 设置中的Preferencescreen不仅可以作为设置界面显示,而且还能够启 ...
- android preferenceactivity 源码,PreferenceActivity PreferenceScreen (3.0/API 11 以前)
Android提供了一个XML驱动的框架,用于为应用程序创建系统样式的PreferenceScreen. (3.0/API 11 以前)是这样使用的 看代码: mypreference.xml and ...
- android开发中PreferenceScreen的使用注意事项
今天开发中需要用到android自带的偏好功能PreferenceScreen 在网上教程很多刚开始设置为 <PreferenceScreen xmlns:android="http: ...
- android中PreferenceScreen类的用法
PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 "偏好显示",明白这个意思就好,就是说根据特点灵活的定义显示内 ...
- Android PreferenceScreen和CheckBoxPreference的用法
最近项目里需要修改Android Settings源码,添加自己的功能,需要用到如下两个 PreferenceScreen和CheckBoxPreference 网上的几个博客很有用,分享给大家 ht ...
- 针对PreferenceScreen的主题设置
因项目需要使用PreferenceScreen,类似系统中的设置功能,但是需要设置自定义背景,修改PreferenceScreen中的文本的字体颜色等:可以以下做法: 1:在values目录下新建st ...
- Android 首选项框架及PreferenceScreen,PreferenceActivity,PreferenceFragment的用法与分析
前言 <Android 4 高级编程(第3版)>第7章第5节讲到了首选项框架和PreferenceActivity概述等内容,因为第一次见到PreferenceScreen,还没用过,所以 ...
- Preference的使用(2) --- PreferenceCategory PreferenceScreen
上一节有讲到Preference的基本使用跟API的介绍 ,没有看的话请先阅读 Preference的使用(1) 现在介绍其子类PreferenceCategory 跟 PreferenceScre ...
- android 4.2 自定义PreferenceCategory PreferenceScreen Preference等
需求: 4.2 原生的catagory通常 是一个title, 一条线, 通常是黑色的或白色的线, 现在有一个需求是 改变title的颜色,改变横线颜色的值. 1. 先定义这个category 的 ...
最新文章
- Opencv-python 图像处理基础知识
- cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)(win7 旗舰版32位)
- python 字符串函数 center_Python字符串处理
- PostgreSQL 空间处理函数
- 【opencv入门篇】 10个程序快速上手opencv【上】
- Android 应用快捷方式 ShortcutManager 简单说明
- flash 随机函数
- win7生成ssh key配置到gitlab
- 企业SOA平台 JBoss SOA
- ViewPage2+TabLayout小记
- 数学建模常用算法案例k—means聚类分析
- ckeditor使用----跳坑之旅
- 漫谈程序员(十三)健康程序猿系列之男人晚睡的7大危害
- 网络广告投放基础,广告
- sip php 来电,php - 如何在Twilio上传递原来的来电显示? (Sip域语音URL配置) - SO中文参考 - www.soinside.com...
- hadoop 报错 java.io.IOException: There appears to be a gap in the edit log. We expected txid 1, but g
- 用canvas绘制微信小程序海报页面并保存相册-适用微信原生
- 用批处理命令打开控制面板选项
- VJ框架 与 人脸检测/物体检测 详解
- vue+iview 兼容IE11浏览器