我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?
当前回答
buttonTint工作为我尝试
android:buttonTint=“@color/white”
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/agreeCheckBox"
android:text="@string/i_agree_to_terms_s"
android:buttonTint="@color/white"
android:layout_below="@+id/avoid_spam_text"/>
其他回答
你可以创建自己的XML在drawable和使用这作为android:background="@drawable/your_xml"
这样你就可以给边角任何东西
<item>
<shape>
<gradient
android:endColor="#fff"
android:startColor="#fff"/>
<corners
android:radius="2dp"/>
<stroke
android:width="15dp"
android:color="#0013669e"/>
</shape>
</item>
如果你打算使用android图标,如上所述..
android:button="@android:drawable/..."
. .这是一个不错的选择,但为了让它工作-我发现你需要添加切换逻辑来显示/隐藏复选标记,就像这样:
checkBoxShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// checkbox status is changed from uncheck to checked.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int btnDrawable = android.R.drawable.checkbox_off_background;
if (isChecked)
{
btnDrawable = android.R.drawable.checkbox_on_background;
}
checkBoxShowPwd.setButtonDrawable(btnDrawable);
}
});
在res-> Drawable下创建一个xml Drawable资源文件,并命名为checkbox_custom_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/checkbox_custom_01_checked_white_green_32" />
<item
android:state_checked="false"
android:drawable="@drawable/checkbox_custom_01_unchecked_gray_32" />
</selector>
上传您的自定义复选框图像文件(我建议png)到您的res->可绘制文件夹。
然后进入布局文件并将复选框更改为
<CheckBox
android:id="@+id/checkBox1"
android:button="@drawable/checkbox_custom_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CheckBox"
android:textSize="32dip"/>
你可以自定义任何东西,只要android:button指向你之前创建的正确的XML文件。
新手注意:虽然这不是强制性的,但是在你的整个布局树中用唯一的id来命名你的复选框是一个很好的做法。
我的目标是使用自定义复选框按钮并删除强调色按钮色调。我尝试了公认的答案,但现在却起作用了,这对我来说很管用:
在styles.xml文件中
<style name="Theme.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:button">@drawable/theme_checkbox_button</item>
<item name="android:drawableLeft">@android:color/transparent</item>
<item name="android:drawablePadding">10dp</item>
<item name="android:buttonTint">@android:color/transparent</item>
<item name="android:buttonTintMode">screen</item>
</style>
现在对于这个问题,唯一需要的属性是android:buttonTint和android:buttonTintMode
下面是一些额外的内容:
我需要使用一个自定义drawable,因此android:button设置了一个选择器drawable 我需要一些空格之间的盒子和文本,因此我遵循这个SO答案,并设置android: drawablleft和android:drawablePadding
100%鲁棒方法。
在我的情况下,我没有访问XML布局源文件,因为我从第三方MaterialDialog库获得复选框。 所以我必须通过编程来解决这个问题。
在xml中创建ColorStateList:
res /颜色/ checkbox_tinit_dark_theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white"
android:state_checked="false"/>
<item android:color="@color/positiveButtonBg"
android:state_checked="true"/>
</selector>
然后应用到复选框: ColorStateList darkStateList = ContextCompat.getColorStateList(getContext(), R.color.checkbox_tint_dark_theme); CompoundButtonCompat。darkStateList setButtonTintList(复选框);
另外,如果有人感兴趣,下面是如何从MaterialDialog对话框中获得复选框(如果你设置了.checkBoxPromptRes(…)):
CheckBox checkbox = (CheckBox) dialog.getView().findViewById(R.id.md_promptCheckbox);
希望这能有所帮助。
推荐文章
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点