使用代码 pre API 21 更改 RadioButtonColor
Change RadioButtonColor using code pre API 21
在我的应用程序中,我使用代码动态创建 RadioButtons
。
RadioGroup radioGroup = new RadioGroup(context);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
RadioButton yesRadioButton = new RadioButton(context);
yesRadioButton.setText("Metallic");
yesRadioButton.setTextColor(ColorStateList.valueOf(0xFF5A5A5A));
yesRadioButton.setTextSize(18);
yesRadioButton.setPadding(0, 0, 180, 0);
我希望将实际按钮(圆圈)的颜色设置为特定颜色:#ED7C02。我已经尝试使用代码行 yesRadioButton.setButtonTintList(ColorStateList.valueOf(0xFFED7C02));
并且当设备是 API 21 (Lollipop) 时这有效。当我在运行旧 API 的设备上尝试它时,我得到以下堆栈跟踪:
09-28 08:15:15.131 14184-14184/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: za.co.gpsts.gpsjobcard, PID: 14184
java.lang.NoSuchMethodError: android.widget.RadioButton.setButtonTintList
是否有其他方法可以实现此目的?我的应用程序使用深色背景,因此很难看到黑色。
预先感谢您的帮助。
您可以为单选按钮使用选择器,这适用于所有 android 版本
在 drawable/radio_button.xml 文件夹下为您的单选按钮创建一个可绘制的选择器,并为您的单选按钮提及所有必需的状态。
并为您的单选按钮添加 属性 android:button="@drawable/radio_button"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:state_window_focused="false"
android:drawable="@drawable/radio_button_on" />
<item
android:state_checked="false"
android:state_window_focused="false"
android:drawable="@drawable/radio_button_off" />
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/radio_button_on_pressed" />
<item
android:state_checked="false"
android:state_pressed="true"
android:drawable="@drawable/radio_button_off_pressed" />
<item
android:state_checked="true"
android:state_focused="true"
android:drawable="@drawable/radio_button_on_selected" />
<item
android:state_checked="false"
android:state_focused="true"
android:drawable="@drawable/radio_button_off_selected" />
<item
android:state_checked="true"
android:drawable="@drawable/radio_button_on" />
<item
android:state_checked="false"
android:drawable="@drawable/radio_button_off" />
</selector>
在我的应用程序中,我使用代码动态创建 RadioButtons
。
RadioGroup radioGroup = new RadioGroup(context);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
RadioButton yesRadioButton = new RadioButton(context);
yesRadioButton.setText("Metallic");
yesRadioButton.setTextColor(ColorStateList.valueOf(0xFF5A5A5A));
yesRadioButton.setTextSize(18);
yesRadioButton.setPadding(0, 0, 180, 0);
我希望将实际按钮(圆圈)的颜色设置为特定颜色:#ED7C02。我已经尝试使用代码行 yesRadioButton.setButtonTintList(ColorStateList.valueOf(0xFFED7C02));
并且当设备是 API 21 (Lollipop) 时这有效。当我在运行旧 API 的设备上尝试它时,我得到以下堆栈跟踪:
09-28 08:15:15.131 14184-14184/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: za.co.gpsts.gpsjobcard, PID: 14184
java.lang.NoSuchMethodError: android.widget.RadioButton.setButtonTintList
是否有其他方法可以实现此目的?我的应用程序使用深色背景,因此很难看到黑色。
预先感谢您的帮助。
您可以为单选按钮使用选择器,这适用于所有 android 版本
在 drawable/radio_button.xml 文件夹下为您的单选按钮创建一个可绘制的选择器,并为您的单选按钮提及所有必需的状态。
并为您的单选按钮添加 属性 android:button="@drawable/radio_button"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:state_window_focused="false"
android:drawable="@drawable/radio_button_on" />
<item
android:state_checked="false"
android:state_window_focused="false"
android:drawable="@drawable/radio_button_off" />
<item
android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/radio_button_on_pressed" />
<item
android:state_checked="false"
android:state_pressed="true"
android:drawable="@drawable/radio_button_off_pressed" />
<item
android:state_checked="true"
android:state_focused="true"
android:drawable="@drawable/radio_button_on_selected" />
<item
android:state_checked="false"
android:state_focused="true"
android:drawable="@drawable/radio_button_off_selected" />
<item
android:state_checked="true"
android:drawable="@drawable/radio_button_on" />
<item
android:state_checked="false"
android:drawable="@drawable/radio_button_off" />
</selector>