如何更改复选框的默认图像

发布于 2024-12-10 04:17:07 字数 860 浏览 0 评论 0原文

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

I am using ListView and for every row I have row_item.xml and I inflate that in code

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

How to change that checkBox use another custom_1 image when is checked and another custom_2 image when is unchecked ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌能量女王 2024-12-17 04:17:07

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

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" />
少年亿悲伤 2024-12-17 04:17:07

checkbox 是一个按钮,因此您可以提供自己的可绘制对象,具有选中、取消选中状态,并将其作为复选框背景。例如

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

checkbox is a button, so you can provide your own drawable with check uncheck state and it as checkbox background. For instance

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

and put this in a file.xml in your drawable folder. In your checkbox:

  <CheckBox
    android:button="@drawable/file"
    android:id="@+id/chk"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
梦与时光遇 2024-12-17 04:17:07

这很容易:)
首先,您需要创建一个 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);

它可以工作,因为我两种方式都使用了它:)并且经过一些调整,它可以工作单选按钮也以同样的方式。快乐编码!

Its pretty easy :)
First you need to create a CustomCheckBox class which will extend CheckBox and override the onDraw(Canvas canvas) method:

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);
    }
}
}

Also create your selector named custom_check_box in your drawable folder:

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

And use your custom icons/imgs in the XML above for all three states (focused/pressed/default)

Use the custom component in your XML like this :

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

and java :

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

It works because I used it both ways :) And with some tweaks it works for RadioButtons too the same way. Happy coding!

慕巷 2024-12-17 04:17:07

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

You can use selector in xml, which is used to change the image of checkbox dynamically according to its checked state.

For example:

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

In the following file, if the checkbox is checked, it will set the ic_linkedin icon and if the checkbox is unchecked, it will set the ic_linkedin_disabled icon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文