【问题标题】:Can i change radio button drawable in android for API < 21?我可以在 android 中为 API < 21 更改可绘制的单选按钮吗?
【发布时间】: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


    【解决方案1】:

    只需从活动主题中删除您的 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>
    

    【讨论】:

      【解决方案2】:

      接受的解决方案对我不起作用。我正在使用 AndroidX 并以 API 17 为目标。无需创建新样式。直接替换

      "android:button"
      

      "app:buttonTint"
      

      【讨论】:

      • 您的解决方案还不够。因为您只能将 buttonTint 与颜色一起使用,而不能与 drawable 一起使用。
      • @oalpayli 不知道该告诉你什么。我在当前项目中使用这种方法来绘制可绘制对象
      【解决方案3】:

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

      我使用 API 18 运行的应用程序也遇到了同样的问题。我无法使用 android:button="@null"android:background="@drawable/&lt;selector&gt;"。这只能从 API21 开始工作。

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

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

      不是这个:

      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 投诉。

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

      在我的 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 按预期工作。

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

      还有这个:

      我现在有了这个:

      还有这个:

      正是我想要的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-04
        • 2015-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多