Android定制radiobutton

时间:2017-10-02 20:26:18

标签: android radio-button

我想制作自定义单选按钮。我已经为普通(radio_normal)状态创建了一个drawable。但我发现很难为check状态创建一个形状(radio_pressed)。我尝试创建我想要的方法。但是没有'好吧。我不知道我是不是以正确的方式做了。对于检查状态,形状应该是这样的。外边框和内圈必须是白色的。可以有人帮我吗? / p>

radio_normal:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false">
    <solid
        android:color="#2196f3"></solid>

</shape>

radio_pressed:

 <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <shape android:shape="oval"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="@android:color/transparent">
        </solid>
        <size
             android:height="60dp"
            android:width="60dp">
       </size>
      <stroke
          android:color="#ffffff"
          android:width="1dp"
          >
    </stroke>
    </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid
                    android:color="#ffffff">
                </solid>
            </shape>
            <size
                android:height="30dp"
                android:width="30dp">
    </size>
        </item>
    </layer-list>

radio_bg:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="true"
    android:drawable="@drawable/radio_pressed"
    >
</item>
    <item
        android:state_checked="false"
        android:drawable="@drawable/radio_normal"
        >
    </item>
</selector>

activity_main:

<RadioButton
        android:id="@+id/radio"
        android:layout_centerInParent="true"
        android:layout_width="10dp"
        android:button="@drawable/radio_bg"
        android:layout_marginTop="100dp"
        android:layout_height="10dp"
        />

2 个答案:

答案 0 :(得分:3)

这就像我能得到的一样接近,希望它能在你身边起作用并且有助于:)

radio_normal:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false">
    <solid android:color="#2196f3" />
    <size
        android:width="25dp"
        android:height="25dp" />
</shape>

radio_pressed:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:padding="3dp">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="ring"
            android:useLevel="false">
            <solid android:color="@android:color/white" />
            <size
                android:width="25dp"
                android:height="25dp" />
        </shape>
    </item>
    <item
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp">
        <shape
            android:shape="oval"
            android:useLevel="false">

            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

radio_bg:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"
        android:drawable="@drawable/radio_pressed"
        >
    </item>
    <item
        android:state_checked="false"
        android:drawable="@drawable/radio_normal"
        >
    </item>
</selector>

activity_main:

<RadioButton
    android:id="@+id/radio"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:button="@drawable/radio_bg"/>

请告诉我这是否适合您:)

答案 1 :(得分:0)

这可能是你想要的吗?

radio_pressed:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="ring"
            android:useLevel="false">
            <solid android:color="@android:color/white" />
            <size
                android:width="10dp"
                android:height="10dp" />
        </shape>
    </item>
    <item
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp">
        <shape
            android:shape="oval"
            android:useLevel="false">

            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

对评论的反应:

You should get this when you change bottom, top, left and right to 50 dp

代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="ring"
            android:useLevel="false">
            <solid android:color="@android:color/white" />
            <size
                android:width="150dp"
                android:height="150dp" />
        </shape>
    </item>
    <item
        android:bottom="50dp"
        android:left="50dp"
        android:right="50dp"
        android:top="50dp">
        <shape
            android:shape="oval"
            android:useLevel="false">

            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

注意:唯一的变化是尺寸可以容纳150dp X 150dp的图像视图。当它用于你的单选按钮时,我非常推荐使用10x10dp尺寸