我想在我的一个项目中改变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>

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

其他回答

适用于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" />

你必须使用以下代码:

<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!

你可以用android:buttonTint属性在XML中这样做:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radio"
    android:checked="false"
    android:padding="5dp"
    android:buttonTint="@color/radio_color"/>

你可以这样做,在Java中使用android:buttonTint:

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

    new int[]{
        DataUtils.getColorResource(mContext, R.color.colorBlack), // Unchecked
        DataUtils.getColorResource(mContext, R.color.colorPrimary) // Checked
    }
);

RadioButton radio = findViewById(R.id.radio);   
radio.setButtonTintList(colorStateList);

@jh314正确。

在文件AndroidManifest.xml中,

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"></application>

在文件style.xml中:

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/red</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

项目名称必须为colorAccent。它决定了应用程序小部件的默认颜色。

但如果你想在代码中改变颜色,也许@aknay的答案是正确的。

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