【问题标题】:How to change default images of CheckBox如何更改复选框的默认图像
【发布时间】:2011-12-08 16:28:42
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    可绘制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>
    

    你的复选框 xml:

    <CheckBox
        android:id="@+id/chk"
        android:button="@drawable/customdrawablecheckbox"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    【讨论】:

    • 我必须以编程方式生成复选框我如何使用自定义复选框有人知道吗?
    • 如果我在复选框上这样做:android:state_checked="false" 默认情况下它不起作用
    • 如何为这个drawable设置内边距?
    • 如何给这张图片添加内边距?
    • 干净整洁,+1。有人可以提供一个答案,包括android默认复选框具有的过渡闪烁效果吗?我想也许可以使用“android:state_pressed”或“android:state_focused”属性所以我玩弄它们,但没有任何运气。
    【解决方案2】:

    checkbox 是一个按钮,因此您可以提供自己的 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>
    

    并将其放入可绘制文件夹中的 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" />
    

    【讨论】:

    • android:background 不会与 checkbutton 图像一起运行,它会将另一个图像放在背景视图上。
    【解决方案3】:

    很简单:) 首先,您需要创建一个 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 中为所有三种状态(聚焦/按下/默认)使用您的自定义图标/图像

    像这样在您的 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);
    

    它之所以有效,是因为我两种方式都使用它 :) 并且通过一些调整,它也适用于 RadioButtons。编码愉快!

    【讨论】:

      【解决方案4】:

      您可以在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图标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多