如何更改CheckBox的默认图像

时间:2011-10-16 10:47:31

标签: android android-layout android-widget

我正在使用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>

如果更改该复选框,则在选中时会使用其他custom_1图像,而在未选中时会使用其他custom_2图像吗?

4 个答案:

答案 0 :(得分:142)

Drawable 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>

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" />

答案 1 :(得分:9)

复选框是一个按钮,因此您可以使用check uncheck状态提供自己的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>

并将其放在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" />

答案 2 :(得分:4)

非常简单:) 首先,您需要创建一个CustomCheckBox类,它将扩展CheckBox并覆盖onDraw(Canvas canvas)方法:

public class CustomCheckBox extends CheckBox {
private final Drawable buttonDrawable;

public CustomCheckBox(Context context, AttributeSet set) {
    super(context, set);
    buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box);
    try {
        setButtonDrawable(android.R.color.transparent);
    } catch (Exception e) {
        // DO NOTHING
    }
    setPadding(10, 5, 50, 5);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    buttonDrawable.setState(getDrawableState());

    final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
    final int height = buttonDrawable.getIntrinsicHeight();
    if (buttonDrawable != null) {
        int y = 0;

        switch (verticalGravity) {
        case Gravity.BOTTOM:
            y = getHeight() - height;
            break;
        case Gravity.CENTER_VERTICAL:
            y = (getHeight() - height) / 2;
            break;
        }

        int buttonWidth = buttonDrawable.getIntrinsicWidth();
        int buttonLeft = getWidth() - buttonWidth - 5;
        buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        buttonDrawable.draw(canvas);
    }
}
}

还在可绘制文件夹中创建名为custom_check_box的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false" 
        android:drawable="@drawable/btn_check_on" />
    <item android:state_checked="false" android:state_window_focused="false"
        android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="true" android:state_pressed="true"
        android:drawable="@drawable/btn_check_on" />    
    <item android:state_checked="false" android:state_focused="true"
        android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="false" android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on" />
</selector>

并在上面的XML中使用您的自定义图标/ imgs用于所有三种状态(聚焦/按下/默认)

在XML中使用自定义组件,如下所示:

<*package + class path*.CustomCheckBox   // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder
            android:text="@string/text"
            android:checked="false" android:layout_width="fill_parent"
            android:id="@+id/myCheckbox" android:layout_height="wrap_content"/>

和java:

private CustomCheckBox mCheckbox;
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox);

它的工作原理是因为我两种方式都使用它:)并且通过一些调整它也可以用于RadioButton。快乐的编码!

答案 3 :(得分:0)

您可以在xml中使用选择器,该选择器用于根据其检查状态动态更改复选框的图像。

例如:

<item android:drawable="@drawable/ic_linkedin" android:state_checked="true" />
<item android:drawable="@drawable/ic_linkedin_disabled" android:state_checked="false" />

在以下文件中,如果选中该复选框,则会设置ic_linkedin图标,如果取消选中该复选框,则会设置ic_linkedin_disabled图标。