自定义复选框偏好设置

15

尽管我已经在XML偏好文件中定义了背景,但我无法自定义我的复选框。我正在尝试显示自定义图像的复选框,并将选择器XML定义为“android_button.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 是位于 res>drawable 文件夹中的两个 .png 图像。

2.我的 Checkbox preference.xml 文件如下:

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

这个定义中有任何错误吗?屏幕上唯一变化的是android:title文本,如果我更改文本,文本确实会改变。其他没有任何变化。我该如何解决这个问题。感谢您的建议。

2个回答

44

实现您需要的有两种方法,第一种是在res/layout中定义自定义复选框布局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"/>

第二种方法是创建自定义主题,在主题中重新定义复选框视图的样式,然后将主题应用到偏好设置活动中,详见如何自定义Dialog中Android复选框CheckMark颜色


2
水平太差了,对android:widgetLayout一无所知,希望其他组件也有这个。 - Gubatron
这个完美地运作了,太感谢你了。我从来不知道我们也可以为小部件使用自定义布局。非常感谢。 - Aritra Roy
工作自定义复选框,但复选框不反映首选项的选中状态 :( - Elye
1
糟糕,我的错。显然我使用了不同的ID名称。为这个绝妙的解决方案点赞! - Elye
1
另外,设置android:background="@null",使复选框的点击真正透明(特别是从棒棒糖的涟漪效果中)。 - Elye
1
一开始我没有仔细注意ID这个属性。将ID设置为此处答案所示非常重要,否则当您单击元素中的任何其他位置时,您将无法看到字段更新! - Aaron

0
创建一个可绘制的 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接