定制PreferenceActivity

android很多设置界面都会使用PreferenceActivity来实现,但那个界面比较丑陋,显示开发总是满足不了要求。

可以自己实现一个,但是那样又会使Activity中的逻辑代码和xml布局文件过于复杂,远远不及PreferenceActivity来的方便快捷。

开发工具:eclipse       运行环境:模拟器 2.2


这个是我模仿360手机安全卫士做的,背景的图片不大一样,懒得去找了,请谅解。


首先看:PreferenceActivity

  1. public class SetupActivity extends PreferenceActivity{  
  2.       
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.setup_layout);  
  7.           
  8.         addPreferencesFromResource(R.xml.setting);  
  9.     }  
  10.       
  11.       
  12. }  
public class SetupActivity extends PreferenceActivity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup_layout);
		
		addPreferencesFromResource(R.xml.setting);
	}
	
	
}



setup_layout.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/mainbg2"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ListView  
  9.         android:id="@android:id/list"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:cacheColorHint="#00000000"  
  13.         android:scrollbarStyle="outsideOverlay" />  
  14.   
  15. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbg2"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:scrollbarStyle="outsideOverlay" />

</LinearLayout>
注意ListView的id,这个不是自己定义的,有兴趣的可以去看下android源码中PreferenceActivity的布局文件。


setting.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <PreferenceCategory  
  5.         android:layout="@layout/sec_pref_category"  
  6.         android:title="设置1" >  
  7.   
  8.         <Preference  
  9.             android:key="sec_password"  
  10.             android:layout="@layout/sec_pref_list_item"  
  11.             android:summary="修改"  
  12.             android:title="点击修改"  
  13.             android:widgetLayout="@layout/sec_pref_widget_more" />  
  14.     </PreferenceCategory>  
  15.   
  16.     <PreferenceCategory  
  17.         android:layout="@layout/sec_pref_category"  
  18.         android:title="设置2" >  
  19.   
  20.         <CheckBoxPreference  
  21.             android:defaultValue="true"  
  22.             android:key="sec_sms_notify"  
  23.             android:layout="@layout/sec_pref_list_item"  
  24.             android:summaryOff="设置2—1提示:关闭"  
  25.             android:summaryOn="设置-1提示:开启"  
  26.             android:title="设置2-1提示"  
  27.             android:widgetLayout="@layout/sec_pref_widget_checkbox" />  
  28.   
  29.         <EditTextPreference  
  30.             android:defaultValue="设置2-2"  
  31.             android:dialogTitle="设置2-2"  
  32.             android:key="sec_sms_notify_text"  
  33.             android:layout="@layout/sec_pref_list_item"  
  34.             android:positiveButtonText="确定"  
  35.             android:summary="点击修改"  
  36.             android:title="设置2-2"  
  37.             android:widgetLayout="@layout/sec_pref_widget_more" />  
  38.     </PreferenceCategory>  
  39.   
  40.     <PreferenceCategory  
  41.         android:layout="@layout/sec_pref_category"  
  42.         android:title="设置3" >  
  43.   
  44.         <CheckBoxPreference  
  45.             android:defaultValue="true"  
  46.             android:key="sec_call_notify"  
  47.             android:layout="@layout/sec_pref_list_item"  
  48.             android:summaryOff="设置3-1提醒:关闭"  
  49.             android:summaryOn="设置3-1提醒:开启"  
  50.             android:title="设置3-1提醒"  
  51.             android:widgetLayout="@layout/sec_pref_widget_checkbox" />  
  52.   
  53.         <EditTextPreference  
  54.             android:defaultValue="设置3-2"  
  55.             android:dialogTitle="设置3-2"  
  56.             android:key="sec_call_notify_text"  
  57.             android:layout="@layout/sec_pref_list_item"  
  58.             android:positiveButtonText="确定"  
  59.             android:summary="点击修改"  
  60.             android:title="设置3-2"  
  61.             android:widgetLayout="@layout/sec_pref_widget_more" />  
  62.     </PreferenceCategory>  
  63.   
  64. </PreferenceScreen>  
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:layout="@layout/sec_pref_category"
        android:title="设置1" >

        <Preference
            android:key="sec_password"
            android:layout="@layout/sec_pref_list_item"
            android:summary="修改"
            android:title="点击修改"
            android:widgetLayout="@layout/sec_pref_widget_more" />
    </PreferenceCategory>

    <PreferenceCategory
        android:layout="@layout/sec_pref_category"
        android:title="设置2" >

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="sec_sms_notify"
            android:layout="@layout/sec_pref_list_item"
            android:summaryOff="设置2—1提示:关闭"
            android:summaryOn="设置-1提示:开启"
            android:title="设置2-1提示"
            android:widgetLayout="@layout/sec_pref_widget_checkbox" />

        <EditTextPreference
            android:defaultValue="设置2-2"
            android:dialogTitle="设置2-2"
            android:key="sec_sms_notify_text"
            android:layout="@layout/sec_pref_list_item"
            android:positiveButtonText="确定"
            android:summary="点击修改"
            android:title="设置2-2"
            android:widgetLayout="@layout/sec_pref_widget_more" />
    </PreferenceCategory>

    <PreferenceCategory
        android:layout="@layout/sec_pref_category"
        android:title="设置3" >

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="sec_call_notify"
            android:layout="@layout/sec_pref_list_item"
            android:summaryOff="设置3-1提醒:关闭"
            android:summaryOn="设置3-1提醒:开启"
            android:title="设置3-1提醒"
            android:widgetLayout="@layout/sec_pref_widget_checkbox" />

        <EditTextPreference
            android:defaultValue="设置3-2"
            android:dialogTitle="设置3-2"
            android:key="sec_call_notify_text"
            android:layout="@layout/sec_pref_list_item"
            android:positiveButtonText="确定"
            android:summary="点击修改"
            android:title="设置3-2"
            android:widgetLayout="@layout/sec_pref_widget_more" />
    </PreferenceCategory>

</PreferenceScreen>
其中的android:widgetLayout="@layout/sec_pref_widget_checkbox"指定checkBox用自定义的checkbox

android:layout="@layout/sec_pref_list_item"指定布局



sec_pref_category.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#ff83a4b4"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@android:id/title"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:paddingLeft="5.0dip"  
  13.         android:textColor="#FFFFFF"  
  14.         android:textSize="14.0dip" />  
  15.   
  16. </LinearLayout>  
<?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:background="#ff83a4b4"
    android:orientation="vertical" >

    <TextView
        android:id="@android:id/title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="5.0dip"
        android:textColor="#FFFFFF"
        android:textSize="14.0dip" />

</LinearLayout>


sec_pref_widget_checkbox:

  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:layout_gravity="center_vertical"  
  7.     android:layout_marginRight="4.0dip"  
  8.     android:button="@drawable/selector_checkbox"  
  9.     android:clickable="false"  
  10.     android:focusable="false" />  
<?xml version="1.0" encoding="UTF-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginRight="4.0dip"
    android:button="@drawable/selector_checkbox"
    android:clickable="false"
    android:focusable="false" />

sec_pref_list_item:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/selector_list_item"  
  6.     android:gravity="center_vertical"  
  7.     android:minHeight="?android:listPreferredItemHeight"  
  8.     android:paddingRight="?android:scrollbarSize" >  
  9.   
  10.     <RelativeLayout  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginBottom="6.0dip"  
  14.         android:layout_marginLeft="15.0dip"  
  15.         android:layout_marginRight="6.0dip"  
  16.         android:layout_marginTop="6.0dip"  
  17.         android:layout_weight="1.0" >  
  18.   
  19.         <TextView  
  20.             android:id="@android:id/title"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:ellipsize="marquee"  
  24.             android:fadingEdge="horizontal"  
  25.             android:singleLine="true"  
  26.             android:textColor="#FFFFFF"  
  27.             android:textSize="18.0sp" />  
  28.   
  29.         <TextView  
  30.             android:id="@android:id/summary"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_alignLeft="@android:id/title"  
  34.             android:layout_below="@android:id/title"  
  35.             android:maxLines="4"  
  36.             android:textColor="#AAAAAA"  
  37.             android:textSize="14.0sp" />  
  38.     </RelativeLayout>  
  39.   
  40.     <LinearLayout  
  41.         android:id="@android:id/widget_frame"  
  42.         android:layout_width="wrap_content"  
  43.         android:layout_height="fill_parent"  
  44.         android:gravity="center_vertical"  
  45.         android:orientation="vertical" />  
  46.   
  47. </LinearLayout>  
<?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="wrap_content"
    android:background="@drawable/selector_list_item"
    android:gravity="center_vertical"
    android:minHeight="?android:listPreferredItemHeight"
    android:paddingRight="?android:scrollbarSize" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6.0dip"
        android:layout_marginLeft="15.0dip"
        android:layout_marginRight="6.0dip"
        android:layout_marginTop="6.0dip"
        android:layout_weight="1.0" >

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textColor="#FFFFFF"
            android:textSize="18.0sp" />

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textColor="#AAAAAA"
            android:textSize="14.0sp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

sec_pref_widget_more:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_alignParentRight="true"  
  6.     android:layout_marginBottom="6.0dip"  
  7.     android:layout_marginLeft="10.0dip"  
  8.     android:layout_marginRight="4.0dip"  
  9.     android:layout_marginTop="6.0dip"  
  10.     android:src="@drawable/pref_arrow_right" />  
<?xml version="1.0" encoding="UTF-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="6.0dip"
    android:layout_marginLeft="10.0dip"
    android:layout_marginRight="4.0dip"
    android:layout_marginTop="6.0dip"
    android:src="@drawable/pref_arrow_right" />


代码下载地址:http://download.csdn.net/detail/luck_apple/3911442


仅供学习使用!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值