制作自定义单选按钮

Making a Custom RadioButton

我正在尝试像这样制作自定义 RadioButton:

我尝试制作一个包含三个 RadioButton 的 RadioGroup,RadioGroup 的背景是一个矩形,RadioButton 的背景在选中时是蓝色矩形,未选中时是白色,但它似乎不起作用.

<RadioGroup
    android:id="@+id/Frequency"
    android:layout_width="370dp"
    android:layout_height="40dp"
    android:background="@drawable/radiorectangle"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.614">

    <RadioButton
        android:id="@+id/Dailyrb"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="Daily" />

    <RadioButton
        android:id="@+id/Weekly"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Weekly" />

    <RadioButton
        android:id="@+id/Monthly"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Monthly" />
</RadioGroup>

按钮

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape android:shape="rectangle">
        <solid android:color="@color/clearBlue">
        </solid>
        <corners android:radius="16dp"></corners>
    </shape>
</item>
<item android:state_checked="false" >
    <shape android:shape="rectangle">
        <solid android:color="@color/white">
        </solid>
        <corners android:radius="16dp"></corners>
    </shape>
</item>

我建议你这个解决方案:

创建两个形状状态:

res/drawable/state_checked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary">
    </solid>
    <corners android:radius="16dp"></corners>
</shape>

res/drawable/state_unchecked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFF">
    </solid>
    <corners android:radius="16dp"></corners>
</shape>

然后为文本和背景颜色定义选择器。

在您的 res/drawable/ 文件夹中创建一个 text_selector.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" 
          android:color="#FFFFFF" /> <!-- text color when checked -->
    <item android:color="#000000" /> <!-- default text color-->
</selector>

在您的 res/drawable/ 文件夹中创建一个 background_selector.xml :

<?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/state_checked" />

    <item android:drawable="@drawable/state_unchecked" />
</selector>

最后,添加背景选择器和文本选择器:

<RadioButton
    android:id="@+id/Dailyrb"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:textColor="@drawable/text_selector"
    android:background="@drawable/background_selector"
    android:text="Daily" />

<RadioButton
    android:id="@+id/Weekly"
    android:layout_width="0dp"
    android:background="@drawable/background_selector"
    android:textColor="@drawable/text_selector"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Weekly" />

<RadioButton
    android:id="@+id/Monthly"
    android:layout_width="0dp"
    android:textColor="@drawable/text_selector"
    android:background="@drawable/background_selector"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Monthly" />

查看结果:

您可以使用默认值ChipGroup included in the Material Components Library

   <com.google.android.material.chip.ChipGroup
            app:singleSelection="true"
            ....>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                app:checkedIconVisible="true"
                android:text="Daily"
                .../>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                app:checkedIconVisible="true"
                android:text="Weekly"
                .../>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                app:checkedIconVisible="true"
                android:text="Monthly"
                .../>

   </com.google.android.material.chip.ChipGroup>