2010-08-25 208 views
14

我无法自定义我的复选框,虽然我已经在xml首选项文件中定义了背景,但并未将其拉出。 1.我想显示复选框,自定义图像和定义选择XML作为“android_button.xml”它看起来像:自定义复选框首选项

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checkable="true" 
     android:drawable="@drawable/state_normal" /> <!-- pressed --> 
<item android:state_checked="true" 
     android:drawable="@drawable/android_pressed" /> <!-- focused --> 
<item android:drawable="@drawable/state_normal" /> <!-- default --> 
</selector> 

state_normal和android_pressed在水库2个巴纽图像>绘制文件夹。

2.my复选框preference.xml文件是:

  <CheckBoxPreference android:key="@string/Drop_Option" 
      android:title="Close after call drop" 
      android:defaultValue="true" 
      android:background="@drawable/android_button" 
      /> 

是否有定义的任何错误,在屏幕上显示出来的唯一变化是android:标题文字,如果我改变文本,它改变了文字。没有其他变化。我该如何解决。谢谢你的建议。

回答

42

有两种方法可以实现你所需要的,首先是定义在水库自定义复选框布局custom_chexbox.xml /布局:

<?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:focusable="false" 
android:clickable="false" android:button="@drawable/android_button"/> 

然后,你需要指定此布局的偏好:

<CheckBoxPreference android:key="@string/Drop_Option" 
android:title="Close after call drop" android:defaultValue="true" 
android:widgetLayout="@layout/custom_checkbox"/> 

第二种方法是创建自定义主题,为复选框视图重新定义样式并将主题应用于首选项活动,有关详细信息,请参阅How to customize the color of the CheckMark color in android in a dialog. : android

+1

生病等级,对android:widgetLayout没有任何线索,希望每个其他组件都有。 – Gubatron 2011-12-20 20:25:32

+0

伙计,这是完美的。 – zenperttu 2012-04-20 20:55:50

+0

这是完美的人。你是一个拯救生命的人。我从来不知道我们也可以使用自定义布局的小部件。非常感谢。 – 2015-01-11 08:09:24

0

让一个drwable XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_enabled="false"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_active_btn" android:state_checked="true" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_enabled="false"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_focused="true"></item> 
    <item android:drawable="@drawable/checkbox_inactive_btn" android:state_checked="false" android:state_pressed="true"></item> 

</selector> 

设置它通过编程 cb.setButtonDrawable(R.drawable.checkboxcustom);