Android之PreferenceActivity(包括样式自定义)


看了网上有许多介绍 PreferenceActivity 的文章都大概只介绍了一些基本的信息,现在我把他们整合了一下。。。

1、PreferenceActivity是什么

PreferenceActivity是android提供的对系统信息和配置进行自动保存的Activity,它通过SharedPreference方式将信息保存在XML 文件当中。使用PreferenceActivity不需要我们对SharedPreference进行操作,系统会自动对Activity 的各种View上的改变进行保存.

2、如何创建PreferenceActivity

创建Android项目,并添加一个Android xml文件。注意,这次选择的不是Layout,而是Preference,而且注意Folder路径是 res/xml。

1、在res/xml/下打开添加的preference.xml文件。

下面我们看看PrefeneceActivity都提供了哪几种元素可供使用。点击Add按钮,在打开的新窗口中可以看到以下几项:

CheckBoxPreference:CheckBox选择项,对应的值的ture或flase。如图:

EditTextPreference:输入编辑框,值为String类型,会弹出对话框供输入。

ListPreference: 列表选择,弹出对话框供选择。

Preference:只进行文本显示,需要与其他进行组合使用。

PreferenceCategory:用于分组。效果如下:

PreferenceScreen:PreferenceActivity的根元素,必须为它。

 

RingtonePreference:系统玲声选择。

 

相应属性分析:

PreferenceCategory属性分析:

title:显示的标题

key:唯一标识(至少在同一程序中是唯一),SharedPreferences也将通过此Key值进行数据保存,也可以通过key值获取保存的信息 (以下相同)。

CheckBoxPreference属性分析:

Key:唯一标识.

title:显示标题(大字体显示)

summary:副标题(小字体显示)

defaultValue:默认值(当然,此处只能是true或false了)

Preference属性分析:

Key:唯一标识.

title:显示标题(大字体显示)

summary:副标题(小字体显示)

dependency:附属,即标识此元素附属于某一个元素(通常为CheckBoxPreference)。dependency值为所附属元素的key。上面代码中的Preference元素附属于key等于“apply_bluetooth”的CheckPreference元素,当CheckPreference值为true时,Preference则为可用,否则为不可用。

EditTextPreperence属性分析:

Key:唯一标识.

title:显示标题(大字体显示)

ListPreference属性分析:

Key:唯一标识.

title:显示标题(大字体显示)

dialogTitle:弹出对话框的标题

entries:列表中显示的值。为一个数组,通读通过资源文件进行设置。

entryValues:列表中实际保存的值,也entries对应。为一个数组,通读通过资源文件进行设置。

2、PreferenceActivity是专门用于显示preference的,所以只要创建一个继承自PreferenceActivity类即可。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 所的的值将会自动保存到 SharePreferences
    addPreferencesFromResource(R.xml.preferences);
}


3、如何响应PreferenceActivity的操作呢?

其实只要重写PreferenceActivity的 onPreferenceTreeClick的方法就可以了,通过参数preference来判断是对那一个元素进行的,并根据需要进行操作。

 测试代码(res/xml/preferences.xml):

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory 
        android:layout="@layout/preference_category"
        android:title="账号管理">
        <Preference
            android:title="用户登入"
            android:layout="@layout/preference"/>
        <Preference
            android:layout="@layout/preference"
            android:title="我的资料"
            android:summary="我的信息,收藏,粉丝"/>
    </PreferenceCategory>
    <PreferenceCategory 
        android:layout="@layout/preference_category"
        android:title="功能设置">
        <CheckBoxPreference 
            android:layout="@layout/preference"
            android:title="左右滑动"
            android:widgetLayout="@layout/preference_checkbox"/>
        <CheckBoxPreference 
            android:layout="@layout/preference"
            android:title="提示声音"
            android:widgetLayout="@layout/preference_checkbox"/>
    </PreferenceCategory>
     <PreferenceCategory 
         android:layout="@layout/preference_category"
        android:title="缓存设置">
        <Preference 
            android:layout="@layout/preference"
            android:title="清除缓存"/>
    </PreferenceCategory>
    <PreferenceCategory 
        android:layout="@layout/preference_category"
	    android:title="其他">
        <Preference 
	        android:title="意见反馈" 
	        android:layout="@layout/preference"
	        android:summary="用户意见反馈"/>
        <Preference 
            android:layout="@layout/preference"
	        android:title="新版本检测" 
	        android:summary="检测是否有新版本"/>
        <Preference 
            android:layout="@layout/preference"
	        android:title="关于我们" 
	        android:summary="关于我们、版权信息"/>
    </PreferenceCategory>
</PreferenceScreen>

3、如何自定义PreferenceActivity

有时候我们不太想要系统给我们的单调的界面风格,如下图


这时我们就需要自定义样式来达到我们想要的效果

那么这个时候有两种选择: 

  1.针对单个应用程序,定义一个cutom的layout,当然这个layout跟系统的layout元素要一致(否则你怎么改呢?),然后在preference.xml(文件名你自己随便取)android:layout添加你定一个 的layout就可以了。

  2.针对整个android系统,修改preference相关的属性,那么所有的用到PreferenceActivity的界面都会相应的改变。这里只需要修改相应的系统layout文件(在framework/base/core/res/res/layout),比如preference_category.xml等。

下面我们先来说说第一种,比较常用到的

首先,定义你的layout文件,这个layout你直接到framework拷下来,这里标记为custom_preference.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" android:layout_height="wrap_content"
     android:minHeight="?android:attr/listPreferredItemHeight"
     android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
 
     <RelativeLayout android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:layout_marginLeft="15dip"
         android:layout_marginRight="6dip" android:layout_marginTop="6dip"
         android:layout_marginBottom="6dip" android:layout_weight="1">
 
         <TextView android:id="@+android:id/title"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
             android:ellipsize="marquee" android:fadingEdge="horizontal"
             android:textColor="#475ad7"/>
 
         <TextView android:id="@+android:id/summary"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
             android:textAppearance="?android:attr/textAppearanceSmall"
             android:textColor="#5ad747"/>
 
     </RelativeLayout>
 
     <!-- Preference should place its actual preference widget here. -->
     <LinearLayout android:id="@+android:id/widget_frame"
         android:layout_width="wrap_content" android:layout_height="match_parent"
         android:gravity="center_vertical" android:orientation="vertical"/>
 
 </LinearLayout>

然后,你在些preference.xml文件的时候,应用到这个layout文件。

 

<?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
     <CheckBoxPreference    android:key="checkbox_key"
         android:title="checkbox_title"
         android:summary="checkbox_summery"
         android:defaultValue="true"
         android:layout="@layout/custom_preference_layout"></CheckBoxPreference>
 </PreferenceScreen>

 

ok,到这里,你就像平时一样使用这个preference.xml文件就好了,你会发现字体的颜色成功改变了

  


下面再说下怎么自定义CheckBoxPreference的CheckBox复选框

系统默认CheckBoxPreference的CheckBox样式


自定义后的CheckBox样式


其实,关键的一步就是指定CheckBoxPreference的android:widgetLayout属性,详细步骤就不说了,下面直接上代码,很简单的。

1./res/xml/my_preference.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <CheckBoxPreference  
  5.         android:key="cbp"  
  6.         android:summaryOff="Off"  
  7.         android:summaryOn="On"  
  8.         android:title="CheckBoxPreference"  
  9.         android:widgetLayout="@layout/my_checkbox" />  
  10.   
  11. </PreferenceScreen>  
2./res/layout/my_checkbox.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+android:id/checkbox"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:button="@drawable/checkbox_checked_style"  
  7.     android:clickable="false"  
  8.     android:focusable="false" />  
3./res/drawable/checkbox_checked_style.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/icon_checkbox_unchecked" android:state_checked="false"/>  
  5.     <item android:drawable="@drawable/icon_checkbox_checked" android:state_checked="true"/>  
  6.   
  7. </selector>  
4.MainActivity.java注意要继承PreferenceActivity

[java]  view plain copy
  1. public class MainActivity extends PreferenceActivity {  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         addPreferencesFromResource(R.xml.my_preference);  
  7.     }  
  8.   
  9.     @Override  
  10.     public boolean onCreateOptionsMenu(Menu menu) {  
  11.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  12.         return true;  
  13.     }  
  14.       
  15. }  

4、PreferenceActivity消除背景

消除preference拖拉时黑色背景方法:

首先建立一个preference_list.xml

代码如下:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView android:id="@android:id/list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="3dip"
       android:divider="@drawable/divider"
       android:cacheColorHint="#00000000"  
       />
</LinearLayout>

注释:android:divider="@drawable/divider"   android:cacheColorHint="#00000000"

重点是这两句话:第一句是设置preferece之间的分割线;第二句就是消除preference拖拽时产生的黑色背景啦!(“#00000000”就是将其设为透明,你也可以把它设为其他颜色)

二、代码中调用方法:

随便写个PreActivity.java

import android.content.Context;
importandroid.preference.PreferenceActivity;
importandroid.preference.PreferenceScreen;
public class PreActivity extendsPreferenceActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.test);
        setContentView(R.layout.preference_list);    //将test布局中的内容添加到该list中,(补充:你也可以在preference_list中添加其他一些控键)
}
}

贴一个test.xml出来:

<?xml version="1.0"encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 
    <CheckBoxPreference
           android:key="test_box"
           android:title="Test Box"
           android:summary="summary"
           android:persistent="false"/>
    <Preference
            android:key="test"
            android:title="Test"
            android:persistent="false"/>
 
</PreferenceScreen>
这样黑色背景就消除了。。。。。

5、下面是我自己根据别的app仿照的一个demo,效果图如下

6、下面上源码

http://download.csdn.net/detail/shenyongjun1209/5276024



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值