【问题标题】:Any way to change the color of a radio button?有什么办法可以改变单选按钮的颜色?
【发布时间】:2011-02-12 13:02:33
【问题描述】:
我正在使用一个包含一组单选按钮的单选组的 android 表单。据我所知,当您选择单选按钮时,无法设置单选按钮突出显示的颜色。它似乎总是默认为一些亮绿色。这是可编辑的还是不可编辑的?
谢谢
【问题讨论】:
标签:
android
radio-button
android-layout
【解决方案1】:
是的,您可以创建自己的可绘制对象,使其在选中时看起来像您想要的样子,并使用 android:button 将其设置为资源。
Example here
【解决方案2】:
使用 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);
【解决方案3】:
在 api 级别 21+ 上,您可以更改 buttonTint
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myId"
android:checked="true"
android:buttonTint="@color/accent"/>