现在的位置: 首页 > 综合 > 正文

Android 自定义CheckBoxPreference的CheckBox复选框

2013年12月04日 ⁄ 综合 ⁄ 共 1126字 ⁄ 字号 评论关闭

在使用Android的Preference,有时为了让我们的界面更加美观,我们会自定义自己的Preference。今天就主要说一下怎样自定义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

抱歉!评论已关闭.