【发布时间】:2017-09-08 17:50:35
【问题描述】:
我正在使用 AppCompatRadioButton,我想为此使用我的自定义可绘制对象,我通过下面的代码实现了这一点,但它仅适用于 API >= 21。
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
selector_custom_radio_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:drawable="@drawable/ic_done_single" android:state_checked="false" />
<item android:drawable="@drawable/ic_done_all" android:state_checked="true" />
</selector>
然后我通过活动主题中的样式设置单选按钮选择器
Styles.xml
<!-- Base application theme. -->
<style name="ThemeNewsFeed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>
</style>
<style name="MyRadioButtonStyle">
<item name="android:button">@drawable/selector_custom_radio_buttons</item>
</style>
这是 AndroidManifest.xml 中的活动
<activity android:name=".social.NewsFeedActivity"
android:theme="@style/ThemeNewsFeed">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:问题是我的自定义可绘制对象在 API 21 及更高版本上运行良好,但在 API 低于 21 时,它显示默认单选按钮可绘制。那我应该为较低的 API 做些什么呢?
【问题讨论】:
标签: android radio-button material-design selector android-drawable