1.Defining Preferences in XML

  Although you can instantiate new Preference objects at runtime, you should define your list of settings in XML with a hierarchy of Preference objects. Using an XML file to define your collection of settings is preferred because the file provides an easy-to-read structure that's simple to update. Also, your app's settings are generally pre-determined, although you can still modify the collection at runtime.

  Each Preference subclass can be declared with an XML element that matches the class name, such as<CheckBoxPreference>.

  You must save the XML file in the res/xml/ directory. Although you can name the file anything you want, it's traditionally named preferences.xml. You usually need only one file, because branches in the hierarchy (that open their own list of settings) are declared using nested instances of PreferenceScreen.

  Note: If you want to create a multi-pane layout for your settings, then you need separate XML files for each fragment.

  The root node for the XML file must be a <PreferenceScreen> element. Within this element is where you add each Preference. Each child you add within the <PreferenceScreen> element appears as a single item in the list of settings.

2.For example

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_sync"
android:title="@string/pref_sync"
android:summary="@string/pref_sync_summ"
android:defaultValue="true" />
<ListPreference
android:dependency="pref_sync"
android:key="pref_syncConnectionType"
android:title="@string/pref_syncConnectionType"
android:dialogTitle="@string/pref_syncConnectionType"
android:entries="@array/pref_syncConnectionTypes_entries"
android:entryValues="@array/pref_syncConnectionTypes_values"
android:defaultValue="@string/pref_syncConnectionTypes_default" />
</PreferenceScreen>

  In this example, there's a CheckBoxPreference and a ListPreference. Both items include the following three attributes:

android:key
This attribute is required for preferences that persist a data value. It specifies the unique key (a string) the system uses when saving this setting's value in the SharedPreferences.

The only instances in which this attribute is not required is when the preference is a PreferenceCategoryor PreferenceScreen, or the preference specifies an Intent to invoke (with an <intent> element) or aFragment to display (with an android:fragment attribute).

android:title
This provides a user-visible name for the setting.
android:defaultValue
This specifies the initial value that the system should set in the SharedPreferences file. You should supply a default value for all settings.

  For information about all other supported attributes, see the Preference (and respective subclass) documentation.

  Figure 2. Setting categories with titles. 
1. The category is specified by the<PreferenceCategory> element. 
2. The title is specified with the android:titleattribute.

  When your list of settings exceeds about 10 items, you might want to add titles to define groups of settings or display those groups in a separate screen. These options are described in the following sections.

Creating setting groups

  If you present a list of 10 or more settings, users may have difficulty scanning, comprehending, and processing them. You can remedy this by dividing some or all of the settings into groups, effectively turning one long list into multiple shorter lists. A group of related settings can be presented in one of two ways:

  You can use one or both of these grouping techniques to organize your app's settings. When deciding which to use and how to divide your settings, you should follow the guidelines in Android Design's Settings guide.

Using titles

  If you want to provide dividers with headings between groups of settings (as shown in figure 2), place each group ofPreference objects inside a PreferenceCategory.

  For example:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_sms_storage_title"
android:key="pref_key_storage_settings">
<CheckBoxPreference
android:key="pref_key_auto_delete"
android:summary="@string/pref_summary_auto_delete"
android:title="@string/pref_title_auto_delete"
android:defaultValue="false"... />
<Preference
android:key="pref_key_sms_delete_limit"
android:dependency="pref_key_auto_delete"
android:summary="@string/pref_summary_delete_limit"
android:title="@string/pref_title_sms_delete"... />
<Preference
android:key="pref_key_mms_delete_limit"
android:dependency="pref_key_auto_delete"
android:summary="@string/pref_summary_delete_limit"
android:title="@string/pref_title_mms_delete" ... />
</PreferenceCategory>
...
</PreferenceScreen>

Using subscreens

  If you want to place groups of settings into a subscreen (as shown in figure 3), place the group of Preferenceobjects inside a PreferenceScreen.

  Figure 3. Setting subscreens. The <PreferenceScreen> element creates an item that, when selected, opens a separate list to display the nested settings.

  For example:

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="button_voicemail_category_key"
android:title="@string/voicemail"
android:persistent="false">
<ListPreference
android:key="button_voicemail_provider_key"
android:title="@string/voicemail_provider" ... />
<!-- opens another nested subscreen -->
<PreferenceScreen
android:key="button_voicemail_setting_key"
android:title="@string/voicemail_settings"
android:persistent="false">
...
</PreferenceScreen>
<RingtonePreference
android:key="button_voicemail_ringtone_key"
android:title="@string/voicemail_ringtone_title"
android:ringtoneType="notification" ... />
...
</PreferenceScreen>
...
</PreferenceScreen>

Using intents

  In some cases, you might want a preference item to open a different activity instead of a settings screen, such as a web browser to view a web page. To invoke an Intent when the user selects a preference item, add an<intent> element as a child of the corresponding <Preference> element.

  For example, here's how you can use a preference item to open a web page:

<Preference android:title="@string/prefs_web_page" >
<intent android:action="android.intent.action.VIEW"
android:data="http://www.example.com" />
</Preference>

  You can create both implicit and explicit intents using the following attributes:

android:action The action to assign, as per the setAction() method.
android:data The data to assign, as per the setData() method.
android:mimeType The MIME type to assign, as per the setType() method.
android:targetClass The class part of the component name, as per the setComponent() method.
android:targetPackage The package part of the component name, as per the setComponent() method.

Android偏好设置(2)为应用定义一个偏好设置xml的更多相关文章

  1. Android中自己定义一个shade.xml

    自己定义一个shade: <shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!- ...

  2. 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Complex(int i,int j) 显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式。 求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Compl

    因标题框有限,题目未显示完整,以下再放一份: 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Compl ...

  3. android穿越之旅--如何弹出一个非比寻常的窗体

    上一篇中介绍了一种闻所未闻在android执行java命令的方法,虽然这是一种非常"高级"的技术,然后并没有什么卵用,因此被移除了博客园首页.实际上也并不是一点用处也没有,对已立即 ...

  4. Android开发自学笔记(Android Studio1.3.1)—2.开始第一个Android应用

    一.前言      使用Android Studio开发Android应用是一件非常简单的事情,因为它会帮你自动完成很多工作.本篇我们主要完成一个单击按钮在文本框显示当前时间的简单应用,借此来演示一下 ...

  5. Android常用控件之Fragment仿Android4.0设置界面

    Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像,用来在一个Activity中描述一些行为或部分用户界面:使用多个Fragment可以在一个单独的Activi ...

  6. Android调用系统相机、自己定义相机、处理大图片

    Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...

  7. android面试题 不仅仅是面试是一个很好的学习

    下面的问题是在网上找到的总结,感谢您分享!希望,我们的共同进步,找到自己心仪的公司,: 1.android dvm 流程和Linux这个过程.无论是应用程序对同一概念: 答案:dvm是dalivk虚拟 ...

  8. Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

    Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...

  9. Android ProgressBar具体解释以及自己定义

       版本号:1.0 日期:2014.5.16 版权:© 2014 kince 转载注明出处   这一次主要说一下Android下的进度条.为什么是它呢,由于最近被其各种美轮美奂的设计所倾倒,计划逐渐 ...

随机推荐

  1. ln 软连接 & 硬连接

    创建软连接的方式 #ln -s soure /file object 创建软连接是连接文件本身,可以跨分区建立软连接,不会应为不同分区而出现不能使用的问题. 在创建软连接的文件中,修改一处文件另一处同 ...

  2. 怎样使用SSH连接OpenStack上的云主机

    转载请注明出处.否则将追究法律责任http://blog.csdn.net/xingjiarong/article/details/47021815 在上一篇博客中我介绍了怎样在OpenStack中创 ...

  3. JavaScript-创建第一个自己的类库

    通过上一节面向对象和原型的学习. 我们知道了怎样创建一个类,包含类的私有化属性和方法.公有化属性和方法.静态属性和方法.在这里略微回想一下.首先要创建一个类能够通过1.new object().2.利 ...

  4. 【教程】怎样申请Chrome应用商店(Web Store)开发人员

    首先你须要一张信用卡,假设你没有的话.能够借用父母或他人的(多见于学生党) 假设你有信用卡.你还得看看信用卡正面是否有注明"VISA"."MasterCard" ...

  5. win64 qt与fortran (codeblocks) 混合编程

    本教程主要解说用fortran生成dll供qt调用(win64) 本教程须要的软件及文件可从以下的连接下载: http://pan.baidu.com/s/1c04jziC fortran我用的软件是 ...

  6. 【ios系列】-数据储存

    第一:plist属性列表 适用对象:仅仅是Foundation框架自带的一些类比如:NString\NSarry\NSDictionary\NSset\NSnumber\NSdata 使用: 1:调用 ...

  7. JAVA 并发编程-返回运行结果(Callable和Future)(九)

    启动一个线程不论使用Thread或者Runnable的时候.都是没有返回结果的. 也就是说Thread和Runnable的run()方法必须没有返回值. public void run(){} 解决方 ...

  8. Unity即将到来的2D工具

    孙广东  2015.7.5 看了一下对功能介绍的视频,确实功能强大. 可是须要FQ在youtube上观看,所以就下载下来了.能够浏览一下: http://www.iqiyi.com/playlist2 ...

  9. Codeforces 709E. Centroids 树形DP

    题目链接:http://codeforces.com/contest/709/problem/E 题意: 给你一棵树,你可以任删一条边和加一条边,只要使得其仍然是一棵树,输出每个点是否都能成为重心 题 ...

  10. RDD变换

    对Key/Value型RDD进行变换 groupBy按Key汇聚 fruit,applevegetable,cucumberfruit,cherryvegetable,beanfruit,banana ...