我为已选中和未选中的单选按钮创建了自定义可绘制对象,但是未显示。似乎省略了背景。我也尝试将背景设置为 custom_radio_button.xml
,但并没有解决问题。背景是透明的,即使我从 @background
中删除了 RadioButtonStyle
属性。
单选按钮:
<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>
答案 0 :(得分:0)
您需要将选择器设置为背景并将按钮设置为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>
同时为选择器设置一个默认的 drawable 并使其成为最后一项
<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>