【问题标题】:How to change the check box (tick box) color to white in android XML如何在android XML中将复选框(复选框)颜色更改为白色
【发布时间】:2021-04-17 16:09:11
【问题描述】:

有没有办法在 android XML 中将复选框(勾选框)颜色更改为白色。 (我需要包含黑色勾号的白色勾选框,作为我在真实设备中的 android studio 中获得的预览)

这是我的复选框代码

<CheckBox
    android:textSize="15sp"
    android:textColor="#fff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save Loging "
    android:id="@+id/checkBox"
    android:layout_below="@id/PasswordeditText"
    android:checked="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:buttonTint="#fff" />

当我添加 android:buttonTint="#fff" 预览时显示我需要的更改,但它在真实设备中不起作用

设计预览

真实设备

有没有像android:buttonTint 这样的属性可以用来实现真实设备的更改。

【问题讨论】:

标签: android xml


【解决方案1】:

colorAccent 设置为您想要的颜色:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorAccent">#fff</item>
</style>

或者,如果您不想更改主主题,请创建一个新主题并将其仅应用于复选框:

<style name="WhiteCheck">
    <item name="colorAccent">#fff</item>
</style>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/WhiteCheck"/>

【讨论】:

  • 使用“colorAccent”创建替代主题需要 API 级别 21 或更高!
  • AppCompat 库支持 @MQoder colorAccent。 API 21 将支持android:colorAccent
【解决方案2】:

这是 tachyonflux 答案的变体:

尝试改变buttonTint:

<style name="my_checkbox_style">
    <item name="buttonTint">#fff</item>
</style>

<CheckBox
            android:id="@+id/my_checkbox"
            style="@style/my_checkbox_style"
            android:background="#000" />

【讨论】:

  • 我的风格中有 buttonTint 和 colorAccent 项目。完全按预期工作。谢谢。
【解决方案3】:

我们可以很容易地通过在 xml 中使用这个属性来改变复选框的颜色

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/COLOR_WHICH_YOU_WANT" />

【讨论】:

    【解决方案4】:

    来自 XML

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/cbCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:buttonTint="@android:color/white" />
    

    或者来自 Java/Kotlin:

    CompoundButtonCompat.setButtonTintList(cbCheck, ColorStateList.valueOf(Color.WHITE));
    

    【讨论】:

      【解决方案5】:

      我们可以使用单行代码更改复选框颜色

      android:buttonTint="@color/app_color" //whatever color
      

      【讨论】:

        【解决方案6】:

        试试这个

        <CheckBox
            android:textSize="15sp"
            android:textColor="#fff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save Loging "
            android:id="@+id/checkBox"
            android:layout_below="@id/PasswordeditText"
            android:checked="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:button="@android:drawable/checkbox_on_background" />
        

        如果选中CheckBox,否则在android:button= 中写入"@android:drawable/checkbox_off_background"

        【讨论】:

          【解决方案7】:
          1. android:buttonTint="@color/white" 这行你的按钮颜色 改变

                      <CheckBox
                        android:id="@+id/checkk"
                        android:layout_width="wrap_content"
                        android:layout_height="@dimen/_40sdp"
                        android:layout_alignParentLeft="true"
                        android:background="@color/black"
                        android:buttonTint="@color/white"                 
                        android:textSize="@dimen/_14sdp" />
            

          【讨论】:

            猜你喜欢
            • 2013-12-18
            • 1970-01-01
            • 2013-12-30
            • 1970-01-01
            • 2012-10-05
            • 2011-08-16
            • 1970-01-01
            • 2021-12-26
            • 1970-01-01
            相关资源
            最近更新 更多