问题 有什么方法可以改变单选按钮的颜色吗?


我正在使用包含一组单选按钮的无线电组的Android表单。据我所知,当您选择它时,无法设置单选按钮突出显示的颜色。它似乎总是默认为一些亮绿色。这是可编辑的还是不可编辑的?

谢谢


1682
2018-04-26 18:40


起源

我觉得给你一个小问题是迟到的,但是你可以检查一下这个问题: stackoverflow.com/a/35610511/1663453 - Sierisimo


答案:


是的,您可以创建自己的drawable,以便在选中时使用它,并使用android:按钮将其设置为资源。

这里的例子


9
2018-04-26 19:02





使用 AppCompatRadioButton 而不是RadioButton。

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

要以编程方式更改颜色,请执行以下操作:

ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {getResources().getColor(R.color.colorPrimary) }
        );

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);

1
2017-07-25 10:54





在api level 21+上你可以改变buttonTint

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

0
2017-10-16 14:50



并且在API小于21或使用兼容性: stackoverflow.com/a/35610511/1663453 - Sierisimo