Android 如何设置复选框边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26447709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:28:44  来源:igfitidea点击:

How to set checkbox border color

androidandroid-layoutandroid-stylesandroid-checkbox

提问by Niaz

The checkbox border is invisible on white background.

复选框边框在白色背景上不可见。

enter image description here

enter image description here

I played with different color parameters without success. I need black border of box. Yes, there examples to make custom checkbox. In all drawable examples the normal box is visible inside of new shape. And the drawable shape is narrow without text in android:text="".

我玩了不同的颜色参数但没有成功。我需要盒子的黑色边框。是的,有制作自定义复选框的示例。在所有可绘制示例中,普通框在新形状内部可见。并且可绘制的形状很窄,没有文字android:text=""

enter image description here

enter image description here

But why checkbox does not look okay in usual xml:

但是为什么复选框在通常的 xml 中看起来不太好:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutBottom1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#FFFFFF"
    android:gravity="center" >
    <CheckBox
        android:id="@+id/checkBottom1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AAAA"
        android:visibility="visible"
        android:textColor="#000000"
        android:checked="true" />

</LinearLayout>

Any ideas? Thanks!

有任何想法吗?谢谢!

回答by Caio Lucas Alcantara

You can use the property

您可以使用该物业

android:buttonTint="what you want"to set your checkbox border color.

android:buttonTint="what you want"设置复选框边框颜色。

回答by IPL10

It's too late to answer but I would like to share what worked for me. Paste below code. It would change the CheckBoxborder color and textColor

现在回答为时已晚,但我想分享对我有用的方法。粘贴下面的代码。它会改变CheckBox边框颜色和textColor

styles.xml

样式文件

<style name="MyCheckBox" parent="Theme.AppCompat.NoActionBar">
    <item name="colorControlNormal">#000</item>   <!-- normal border color change as you wish -->
    <item name="colorControlActivated">#000</item> <!-- activated color change as you wish -->
    <item name="android:textColor">#FFFF3F3C</item> <!-- checkbox text color -->
</style>

now in your main_activity.xmlplace below CheckBoxcode

现在在你的main_activity.xmlCheckBox代码下面

<CheckBox android:id="@+id/check_agree"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:text="I agree"
          android:theme="@style/MyCheckBox"/>   <!-- here apply your checkbox style -->

if above style not working then replace parent theme parent="Theme.AppCompat.NoActionBar"with parent="Theme.AppCompat.Light". Hope it would work.

如果上述样式不起作用,则将父主题替换parent="Theme.AppCompat.NoActionBar"parent="Theme.AppCompat.Light". 希望它会起作用。

回答by Antrromet

Did you try going through here, hereand here?
And as per answering your question

你有没有尝试过这里这里这里
根据回答你的问题

But why checkbox does not look okay in usual xml

Thats because sometimes, the android graphical view is not able to render the custom views, in that case you need to run the code on the emulator or the device to test it out.

那是因为有时,android 图形视图无法呈现自定义视图,在这种情况下,您需要在模拟器或设备上运行代码以对其进行测试。

UPDATEIn case you dont want to use drawables, then you can also define the drawable shape in xml like

更新如果您不想使用可绘制对象,那么您还可以在 xml 中定义可绘制形状,例如

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >

        <solid android:color="#ffffff" >
        </solid>

        <stroke
            android:width="2dp"
            android:color="#ff0000" >
        </stroke>
<corners android:radius="5dp" />

    <padding
        android:bottom="4dp"
        android:left="4dp"
        android:right="4dp"
        android:top="4dp" />
    </shape>

回答by Benjamin Basmaci

None of the answers above worked for me, but this onedid. It suggests using a selector and applying it to the checkbox, which worked perfectly for me.

上面的答案都不对我有用,但这个答案对我有用。它建议使用选择器并将其应用于复选框,这对我来说非常有效。

This does not explain the odd look of the checkbox as shown above, which is why I wouldn't mark this question as a duplicate, but I think it still works as a solution.

这并不能解释上面显示的复选框的奇怪外观,这就是为什么我不会将此问题标记为重复的原因,但我认为它仍然可以作为解决方案。

回答by Jwala Kumar

You can use either of them:

您可以使用其中任何一个:

app:buttonTint="color code here"

or

或者

android:buttonTint="color code here"