【问题标题】:Android : Change checkbox color (boxbackground and tick mark color)Android:更改复选框颜色(框背景和刻度线颜色)
【发布时间】:2020-04-13 09:04:13
【问题描述】:

我正在使用下面的复选框

我的代码...

   <CheckBox
    android:id="@+id/checkBox10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_weight="1"
    android:button="@null"
    android:checked="true"
    android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center_horizontal"
    android:text="10"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:theme="@style/MyCheckBox"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

style.xml

 <style name="MyCheckBox" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">#ffffff</item>
    <item name="colorControlActivated">#ff0000</item>       
 </style>

我可以通过更改样式 xml 中的颜色来更改框颜色。正如我正在使用的那样

 android:button="@null"
 android:drawableBottom="?android:attr/listChoiceIndicatorMultiple" 

我找不到更改刻度颜色的方法。 我需要更改作为背景颜色的刻度颜色(蓝色)。 我该怎么做?

【问题讨论】:

标签: android user-interface checkbox styles


【解决方案1】:

res\drawables\ 文件夹中创建一个名为 cb_selector.xml 的选择器 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/checked" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>

在您的布局文件中将此文件应用到您的复选框

<CheckBox
    android:id="@+id/cb"
    android:text="My CheckBox"
    android:button="@drawable/cb_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在您的可绘制文件夹中添加unchecked.pngchecked.png。这些是复选框的选中和未选中图像。

您也可以使用color 代替drawable

【讨论】:

  • 这样做会让我们失去漂亮的动画效果
【解决方案2】:

我通过使用带有 app:buttonCompat="@drawable/your_selector_drawable" 的 AppCompatCheckBox 实现了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 2020-05-19
    • 2018-06-30
    • 2011-08-17
    • 1970-01-01
    • 2015-05-17
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多