我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?
当前回答
我遇到了同样的问题,我用下面的技术得到了一个解决方案。将btn_check.xml从android-sdk/platforms/android-#(version)/data/res/drawable复制到项目的drawable文件夹,并将“on”和“off”图像状态更改为自定义图像。 然后你的xml将只需要android:button="@drawable/btn_check"
<CheckBox
android:button="@drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
如果你想使用不同的默认Android图标,你可以使用:
android:button="@android:drawable/..."
其他回答
我更喜欢我创建的这个扩展函数:
fun CheckBox.setTint(@ColorInt color: Int?) {
if (color == null) {
CompoundButtonCompat.setButtonTintList(this, null)
return
}
CompoundButtonCompat.setButtonTintMode(this, Mode.SRC_ATOP)
CompoundButtonCompat.setButtonTintList(this, ColorStateList.valueOf(color))
}
它非常类似于我的ImageView,以防有人也想要它:
fun ImageView.setTint(@ColorInt color: Int?) {
if (color == null) {
ImageViewCompat.setImageTintList(this, null)
return
}
ImageViewCompat.setImageTintMode(this, Mode.SRC_ATOP)
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color))
}
我的minSdkVersion是15,我的BaseAppTheme父是Theme.AppCompat.Light.NoActionBar,我正在以编程方式创建我的复选框。下面的步骤对我很有效。
1. 在Java代码中进行更改
CheckBox checkBox = new CheckBox(context);
to
AppCompatCheckBox checkBox = new AppCompatCheckBox(context);
2. 在你的styles.xml中,添加:
<style name="MyCheckboxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="buttonTint">@color/primary_dark</item>
</style>
3.最后,在你的BaseAppTheme(或AppTheme)样式中,添加:
<item name="checkboxStyle">@style/MyCheckboxStyle</item>
<item name="android:checkboxStyle">@style/MyCheckboxStyle</item>
在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来命名你的复选框是一个很好的做法。
如果你打算使用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);
}
});
您可以在“colors.xml”中使用以下两个属性
<color name="colorControlNormal">#eeeeee</color>
<color name="colorControlActivated">#eeeeee</color>
colorControlNormal用于复选框的正常视图,colorControlActivated用于复选框被选中时的视图。
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?