首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未显示单选按钮自定义选择器

未显示单选按钮自定义选择器
EN

Stack Overflow用户
提问于 2020-12-21 22:29:15
回答 1查看 256关注 0票数 0

我为checked and not checked单选按钮创建了自定义的drawable,但是,它没有显示。背景似乎被省略了。我也尝试将后台设置为custom_radio_button.xml,但没有解决问题。背景是透明的,即使我从RadioButtonStyle中删除了@background属性。

RadioButton:

代码语言:javascript
运行
复制
<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">

RadioButtonStyle:

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2020-12-21 22:39:12

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

代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65394546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档