如何更改CheckBox的默认图像

Dam*_*mir 43 android android-widget android-layout

我正在使用ListView,每行我都有row_item.xml,并在代码中对其进行了膨胀

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <CheckBox
        android:id="@+id/chk"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txtChoice"
        android:textColor="#FF0000"
        android:text="TEST"
        android:layout_toRightOf="@id/chk"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /> 
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如何更改该复选框在选中时使用另一个custom_1图像,如果未选中则更改另一个custom_2图像?

A.Q*_*oga 144

可绘制的customdrawablecheckbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="false" android:drawable="@drawable/unchecked_drawable" />
     <item android:state_checked="true" android:drawable="@drawable/checked_drawable" />
     <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state -->
</selector>
Run Code Online (Sandbox Code Playgroud)

yourcheckbox xml:

<CheckBox
    android:id="@+id/chk"
    android:button="@drawable/customdrawablecheckbox"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)


Bla*_*elt 9

复选框是一个按钮,因此您可以提供自己的drawable,并选中取消选中状态,并将其作为复选框背景.例如

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/yourdrawable1" />
<item android:state_checked="true" android:drawable="@drawable/yourdrawable2" />
<item android:drawable="@drawable/yourdrawable1" /> <!-- default -->
 </selector>
Run Code Online (Sandbox Code Playgroud)

并将其放在drawable文件夹中的file.xml中.在您的复选框中:

  <CheckBox
    android:button="@drawable/file"
    android:id="@+id/chk"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)