我想在我的一个项目中改变RadioButton的圆圈的颜色,但我不知道该设置哪个属性。背景颜色是黑色的,所以它是看不见的。我想把圆圈的颜色设置为白色。


当前回答

在res/values/colors.xml文件中,RadioButton默认采用colorAccent的颜色。 到那个文件中修改值

< [name = " colorAccent > # 3F51B5 < / [>

到你想要的颜色。

其他回答

Declare a custom style in your styles.xml file. <style name="MyRadioButton" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/indigo</item> <item name="colorControlActivated">@color/pink</item> </style> Apply this style to your RadioButton via the android:theme attribute. <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Radio Button" android:theme="@style/MyRadioButton"/>

但前提是你的activity扩展了AppCompatActivity。

设置buttonTint属性。例如,android:buttonTint="#99FF33"。

这个kotlin扩展

fun RadioButton.setLangRadioColor(isCheck: Boolean) { val color = if (isCheck) { intArrayOf( ContextCompat.getColor(rootView.context, R.color.light_red), ContextCompat.getColor(rootView.context, R.color.light_red) ) } else { intArrayOf( ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor), ContextCompat.getColor(rootView.context, R.color.sortRadioUnselectColor) ) } val colorStateList = ColorStateList( arrayOf( intArrayOf(-android.R.attr.state_enabled), // disabled intArrayOf(android.R.attr.state_enabled) // enabled ), color ) this.buttonTintList = colorStateList }

设置buttonTint颜色更简单(只适用于API级别21或以上):

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio"
    android:checked="true"
    android:buttonTint="@color/your_color"/>

在values/colors.xml文件中,输入颜色,在本例中是红色:

<color name="your_color">#e75748</color>

结果:

如果你想通过代码(也是API 21及以上):

if(Build.VERSION.SDK_INT >= 21)
{
    ColorStateList colorStateList = new ColorStateList(
            new int[][]
            {
                new int[]{-android.R.attr.state_enabled}, // Disabled
                new int[]{android.R.attr.state_enabled}   // Enabled
            },
            new int[]
            {
                Color.BLACK, // disabled
                Color.BLUE   // enabled
            }
        );

    radio.setButtonTintList(colorStateList); // set the color tint list
    radio.invalidate(); // Could not be necessary
}

如果你有android:buttonTint,它不会工作,你必须改变它到app:buttonTint。 在升级到androidx之后,我不得不这样做。