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. }  

 

 

 

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>  

注意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>  

其中的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>  



 

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" />  


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>  


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" />  



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

 

http://blog.csdn.net/luck_apple/article/details/7064004

posted @ 2014-02-24 00:32  萧萧  阅读(170)  评论(0)    收藏  举报