【问题标题】:Radio Button custom selector is not shown未显示单选按钮自定义选择器
【发布时间】:2020-12-22 10:00:08
【问题描述】:

我为选中和未选中的单选按钮创建了自定义可绘制对象,但是没有显示。好像背景被省略了。我也尝试将背景设置为custom_radio_button.xml,但并没有解决问题。即使我从RadioButtonStyle 中删除@background 属性,背景也是透明的。

单选按钮:

<RadioButton
  android:id="@+id/both_gender_button"
  style="@style/RadioButton"
  android:layout_width="match_parent"
  android:layout_height="@dimen/big_button_height"
  android:layout_gravity="center"
  android:layout_marginTop="@dimen/space_l"
  android:checked="false"
  android:gravity="center"
  android:paddingStart="@dimen/space_m"
  android:paddingEnd="@dimen/space_m"
  android:text="@string/women_and_males"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintHorizontal_bias="0.361"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toBottomOf="@+id/woman_gender_button">

单选按钮样式:

<style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
  <item name="android:button">@drawable/custom_radio_button</item>
  <item name="android:background">@android:color/transparent</item>
  <item name="fontFamily">@font/futura_medium</item>
  <item name="android:textColor">@color/black</item>
  <item name="android:textSize">@dimen/font_xl</item>
</style>

custom_radio_button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/black_stroke_rectangle" android:state_checked="false" />
  <item android:drawable="@drawable/black_stroke_rectangle" android:state_focused="false" />
  <item android:drawable="@drawable/black_stroke_rectangle_white_fill" android:state_checked="true" />
  <item android:drawable="@drawable/black_stroke_rectangle_white_fill" android:state_pressed="true" />
  <item android:drawable="@drawable/black_stroke_rectangle_white_fill" android:state_focused="true" />
</selector>

【问题讨论】:

    标签: android radio-button selector drawable


    【解决方案1】:

    您需要将选择器设置为背景,并将按钮设置为null

    <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">null</item>
        <item name ="android:background">@drawable/custom_radio_button</item>
        <item name="fontFamily">@font/futura_medium</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">@dimen/font_xl</item>
    </style>
    

    还为选择器设置一个默认可绘制对象并将其设为最后一项

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/black_stroke_rectangle" android:state_checked="false" />
    . . . 
    <item android:drawable="@drawable/black_stroke_rectangle_white_fill" android:state_focused="true" />
    <item android:drawable="@drawable/black_stroke_rectangle_default" />
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多