无法创建自定义单选按钮

时间:2014-03-17 15:43:31

标签: android

我尝试创建自定义单选按钮但效果不佳,使用以下代码: custom_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_on" android:state_checked="true"/>
    <item android:drawable="@drawable/radio_on" android:state_pressed="true"/>
    <item android:drawable="@drawable/radio_off"/>

</selector>

和main.xml

<RadioGroup
                    android:id="@+id/radioSex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:orientation="vertical" >

                    <RadioButton
                        android:id="@+id/male"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="@string/male"
                        android:background="@xml/custom_radio"
                        android:textColor="@color/white" />

                    <RadioButton
                        android:id="@+id/female"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/female"
                        android:background="@xml/custom_radio"
                        android:textColor="@color/white" />
                </RadioGroup>

结果如下: enter image description here

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

添加

 android:button="@null"

RadioButton。像xml一样的drawable也应放在drawable文件夹