Java 更改 Checkbox for Android 的选中和未选中图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3192173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:16:23  来源:igfitidea点击:

Change icons of checked and unchecked for Checkbox for Android

javaandroidcheckboxicons

提问by Mohit Deshpande

Instead of having a check mark for the icon, I want a custom star (I have checked and unchecked icons). Can this be done through a property? Or must I declare a custom widget that derives from Checkbox?

我想要一个自定义星标,而不是图标的复选标记(我已经选中和未选中的图标)。这可以通过财产来完成吗?还是我必须声明一个源自 Checkbox 的自定义小部件?

采纳答案by ggomeze

Kind of a mix:

一种混合:

Set it in your layout file :-

将其设置在您的布局文件中:-

 <CheckBox android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:text="new checkbox"
           android:background="@drawable/checkbox_background" 
           android:button="@drawable/checkbox" />

where the @drawable/checkbox will look like:

@drawable/checkbox 看起来像:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:state_focused="true"
  android:drawable="@drawable/checkbox_on_background_focus_yellow" />
 <item android:state_checked="false" android:state_focused="true"
  android:drawable="@drawable/checkbox_off_background_focus_yellow" />
 <item android:state_checked="false"
  android:drawable="@drawable/checkbox_off_background" />
 <item android:state_checked="true"
  android:drawable="@drawable/checkbox_on_background" />
</selector>

回答by Ravi Vyas

One alternative would be to use a drawable/textview instead of a checkbox and manipulate it accordingly. I have used this method to have my own checked and unchecked images for a task application.

一种替代方法是使用可绘制/文本视图而不是复选框并相应地对其进行操作。我已经使用这种方法为任务应用程序创建了自己的已选中和未选中的图像。

回答by suomi35

I realize this is an old question, and the OP is talking about using custom gx that aren't necessary 'checkbox'-looking, but there is a fantastic resource for generating custom colored assets here: http://kobroor.pl/

我意识到这是一个老问题,OP 正在谈论使用不需要“复选框”外观的自定义 gx,但是这里有一个用于生成自定义彩色资产的绝佳资源:http: //kobroor.pl/

Just give it the relevant details and it spits out graphics, complete with xml resources, that you can just drop right in.

只要给它相关的细节,它就会输出图形,完整的 xml 资源,你可以直接插入。

回答by алекс кей

it's android:button="@drawable/selector_checkbox"to make it work

这是android:button="@drawable/selector_checkbox"为了让它工作

回答by giovaniguerra

This may be achieved by using AppCompatCheckBox. You can use app:buttonCompat="@drawable/selector_drawable"to change the selector.

这可以通过使用来实现AppCompatCheckBox。您可以使用app:buttonCompat="@drawable/selector_drawable"来更改选择器。

It's working with PNGs, but I didn't find a way for it to work with Vector Drawables.

它适用于 PNG,但我没有找到适合 Vector Drawables 的方法。