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


当前回答

您可以使用XML中的样式来更改单选按钮的未选中和已选中状态的颜色。

<RadioButton
    android:id="@+id/rb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/RadioButtonStyle" />

在style.xml

<style name="RadioButtonStyle" parent="Theme.AppCompat.Light">
        <item name="colorAccent">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
</style>

您可以在此样式中设置所需的颜色。

其他回答

你必须使用以下代码:

<android.support.v7.widget.AppCompatRadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Radiobutton1"
                    app:buttonTint="@color/black" />

使用app:buttonTint而不是android:buttonTint和android.support.v7.widget。AppCompatRadioButton代替Radiobutton!

使用app:buttonTint而不是android:buttonTint,像这样:

 <com.google.android.material.radiobutton.MaterialRadioButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="English"
        android:checked="true"
        app:buttonTint="#FF0000"
        android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
        android:layout_marginHorizontal="16dp"
        android:layoutDirection="rtl"
        />

or

<RadioButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="English"
    android:checked="true"
    app:buttonTint="#FF0000"
    android:textAppearance="@style/TextAppearance.Material3.TitleSmall"
    android:layout_marginHorizontal="16dp"
    android:layoutDirection="rtl"
    />

适用于api21

创建一个自定义样式的RadioButton:

文件style.xml

<style name="RadioButton" parent="Theme.AppCompat.Light">
    <item name="colorAccent">@color/green</item>
    <item name="android:textColorSecondary">@color/mediumGray</item>
    <item name="colorControlNormal">@color/red</item>
</style>

在布局中,使用主题:

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

适用于API 21及以上

只需使用buttonTint:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/green" />

这个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 }

更新:

用这个代替 < android.support.v7.widget.AppCompatRadioButton android: id =“@ + id / rbtn_test” android: layout_width = " wrap_content " android: layout_height = " wrap_content " 应用:buttonTint = " @color /主" / > 然后将这一行添加到父布局或在Android Studio中按Alt + Enter自动添加 xmlns:应用= " http://schemas.android.com/apk/res-auto "

最小示例应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbtn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:buttonTint="@color/primary" />

</LinearLayout>

在你的程序中,你应该这样调用它: AppCompatRadioButton radioButton = (AppCompatRadioButton) view.findViewById(R.id.rbtn_test);

基本上,这种模式可以应用于所有AppCompact类型,如AppCompatCheckBox、AppCompatButton等。

旧的回答:

为了支持以下android API 21,你可以使用AppCompatRadioButton。然后使用setSupportButtonTintList方法来改变颜色。这是我创建单选按钮的代码片段。

    AppCompatRadioButton rb;
    rb = new AppCompatRadioButton(mContext);

    ColorStateList colorStateList = new ColorStateList(
            new int[][]{
                    new int[]{-android.R.attr.state_checked},
                    new int[]{android.R.attr.state_checked}
            },
            new int[]{

                    Color.DKGRAY
                    , Color.rgb (242,81,112),
            }
    );
    rb.setSupportButtonTintList(colorStateList);

API 19的测试结果:

详见Android参考链接。