如何将android XML中的复选框(复选框)颜色更改为白色

时间:2021-06-29 07:31:32

Is there any way to change the check box (tick box) color to white in android XML. (I need white color tick box which contain black tick, as the preview I got in android studio inside my real device)

有没有办法将android XML中的复选框(勾号)颜色改为白色?(我需要白色的勾选框,里面有黑色的勾选,就像我在android studio里的预览一样)

Here is my code for check box

这是我的复选框代码

<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" />

When I add android:buttonTint="#fff" preview show the change I need, but it doesn't work in real device

当我添加android:buttonTint="#fff"时,预览会显示我需要的更改,但在真正的设备中不能工作

Design preview

设计预览

如何将android XML中的复选框(复选框)颜色更改为白色

Real Device

真实的设备

如何将android XML中的复选框(复选框)颜色更改为白色

Is there any attribute like android:buttonTint which I can use to achieve the changes in real device.

有没有像android:buttonTint这样的属性,我可以用它来实现真实设备的变化。

6 个解决方案

#1


42  

Set the colorAccent to your desired color:

设置颜色重音到你想要的颜色:

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

Or if you don't want to change your main theme, create a new theme and apply it only to the checkbox:

或者,如果你不想改变你的主题,你可以创建一个新的主题,只应用到复选框中:

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

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

#2


9  

This is variation of tachyonflux's answer:

这是tachyonflux的回答的变化:

Try to change buttonTint:

试图改变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" />

#3


3  

easily we can change checkbox color by using this property in xml

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

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

#4


1  

<CheckBox
    android:id="@+id/cbCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/colorPrimary" />

For Api < 21

Api < 21

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cbCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:buttonTint="@color/colorPrimary" />

#5


0  

Try this

试试这个

<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" />

if CheckBox checked, else in android:button= write "@android:drawable/checkbox_off_background"

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

#6


0  

  1. android:buttonTint="@color/white" this line of your button color change

    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" />
    

#1


42  

Set the colorAccent to your desired color:

设置颜色重音到你想要的颜色:

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

Or if you don't want to change your main theme, create a new theme and apply it only to the checkbox:

或者,如果你不想改变你的主题,你可以创建一个新的主题,只应用到复选框中:

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

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

#2


9  

This is variation of tachyonflux's answer:

这是tachyonflux的回答的变化:

Try to change buttonTint:

试图改变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" />

#3


3  

easily we can change checkbox color by using this property in xml

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

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

#4


1  

<CheckBox
    android:id="@+id/cbCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/colorPrimary" />

For Api < 21

Api < 21

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cbCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:buttonTint="@color/colorPrimary" />

#5


0  

Try this

试试这个

<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" />

if CheckBox checked, else in android:button= write "@android:drawable/checkbox_off_background"

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

#6


0  

  1. android:buttonTint="@color/white" this line of your button color change

    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" />