我为checked and not checked单选按钮创建了自定义的drawable,但是,它没有显示。背景似乎被省略了。我也尝试将后台设置为custom_radio_button.xml
,但没有解决问题。背景是透明的,即使我从RadioButtonStyle
中删除了@background
属性。
RadioButton:
<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:
<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>
发布于 2020-12-21 22:39:12
您需要将选择器设置为背景,并将按钮设置为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>
https://stackoverflow.com/questions/65394546
复制相似问题