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


当前回答

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

其他回答

@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="@drawable/radiobutton"在radiobutton的XML内容和你的radiobutton. XML文件将是:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#1E88E5"/>
    <item android:state_checked="true" android:color="#00e676"/>
    <item android:color="#ffffff"/>
</selector>

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

你必须使用以下代码:

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

要以编程方式更改单选按钮的颜色,您可以使用以下方法:

yourradio button name.buttonDrawable?.setColorFilter(Color.parseColor( color_value), PorterDuff.Mode.SRC_ATOP)