15

我无法自定义我的复选框,虽然我已经在 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 文本,如果我更改文本,它会更改文本。没有其他变化。我该如何解决。谢谢你的建议。

4

2 回答 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"/>

第二种方法是创建自定义主题,重新定义复选框视图的样式并将主题应用于首选项活动,请参阅如何在对话框中自定义 android 中 CheckMark 颜色的颜色。: 安卓了解详情。

于 2010-08-26T02:36:59.627 回答
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); 以编程方式设置它

于 2016-06-30T07:13:31.977 回答