API16中如何给RadioButton设置透明背景?
How to set transparent background for RadioButton in API 16?
我有一个单选按钮,它的不同状态具有自定义背景。为了使用这个背景,我将 android:button
属性设置为 transparent
。似乎这在 API 16 中还没有得到支持,并且背景只显示为黑色。
如何才能达到API17及以上的效果?
radio_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
<solid android:color="@color/grey"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
<solid android:color="@color/grey"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
</shape>
</item>
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your choice"
android:textSize="12sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:checked="true"
android:text="Easy"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:text="Middle"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:text="Hard"/>
</RadioGroup>
</LinearLayout>
截图API16
截图API17
将 <solid android:color="#00000000"/>
设置为可绘制背景中第三个 <item>
的形状,并从 AppCompatRadioButton
中删除 android:button
属性。我建议使用您的自定义颜色,因为实践表明 android 颜色可能因设备而异。
我有一个单选按钮,它的不同状态具有自定义背景。为了使用这个背景,我将 android:button
属性设置为 transparent
。似乎这在 API 16 中还没有得到支持,并且背景只显示为黑色。
如何才能达到API17及以上的效果?
radio_button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
<solid android:color="@color/grey"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
<solid android:color="@color/grey"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dp"/>
<stroke android:width="1dp" android:color="@color/grey"/>
</shape>
</item>
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your choice"
android:textSize="12sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:checked="true"
android:text="Easy"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:text="Middle"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="8dp"
android:padding="8dp"
android:button="@android:color/transparent"
android:background="@drawable/radio_button_background"
android:gravity="center"
android:text="Hard"/>
</RadioGroup>
</LinearLayout>
截图API16
截图API17
将 <solid android:color="#00000000"/>
设置为可绘制背景中第三个 <item>
的形状,并从 AppCompatRadioButton
中删除 android:button
属性。我建议使用您的自定义颜色,因为实践表明 android 颜色可能因设备而异。