我可以为 API < 21 更改 android 中可绘制的单选按钮吗?

Can i change radio button drawable in android for API < 21?

我正在使用 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>

然后我通过 activity 主题中的样式设置单选按钮选择器

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>

这里是 activity 在 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 做些什么呢?

只需从 activity 主题中删除您的 AppCompatRadioButton 样式并直接设置为 AppCompatRadioButton xml 代码如下

 <android.support.v7.widget.AppCompatRadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     style="@style/MyRadioButtonStyle""/>

并在 Style.xml

 <style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/selector_custom_radio_buttons</item>
    </style>

接受的解决方案对我不起作用。我正在使用 AndroidX 并定位 API 17。无需创建新样式。只需更换

"android:button"

"app:buttonTint"

确实,这是一个旧线程,但也许其他遇到同样问题的人可能会得到以下帮助。我会给它一个机会...

我的应用程序也遇到了同样的问题 运行 API 18。我无法使用 android:button="@null"android:background="@drawable/<selector>"。那只在 API21 之后有效。

我的解决方案是简单地创建一个新的 class 来扩展 RadioButton class,但是 而不是 继承自 androidx.appcompat.widget.AppCompatRadioButton 但是简单地继承自 RadioButton 本身。

所以,我的“扩展”class 看起来非常非常简单:

不是这个:

public class RadioButtonZed extends  androidx.appcompat.widget.AppCompatRadioButton {
    public RadioButtonZed(Context context, AttributeSet attrs) {
       super(context, attrs);
    }
}

但是这个:

@SuppressLint("AppCompatCustomView")
public class RadioButtonZed extends RadioButton {
    public RadioButtonZed(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

注意 @SuppressLint("AppCompatCustomView") 避免被 AS 投诉 IDE.

我唯一需要做的就是在我的“自己的”单选按钮中添加一个而且只有一个构造函数 class...

在我的 XML 文件中,我更改了这个:

<RadioButton
    android:id="@+id/rb_way_too_much"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_centerVertical="true"
    android:layout_marginStart="13dp"
    android:layout_toEndOf="@+id/tv_level_1"
    android:background="@drawable/radio"
    android:button="@null"
    tools:checked="true" />

对此:

<be.geertvancompernolle.mycustomradiobutton.RadioButtonZed
    android:id="@+id/rb_way_too_much"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_centerVertical="true"
    android:layout_marginStart="13dp"
    android:layout_toEndOf="@+id/tv_level_1"
    android:background="@drawable/radio"
    android:button="@null"
    tools:checked="true" />

这一次,@null@drawable/radio 按预期工作。

最后,而不是这个(注意我的按钮图像“内部”的原始按钮图像圆圈):

还有这个:

我现在有这个:

还有这个:

正是我想要的!