【问题标题】:Making a Custom RadioButton制作自定义单选按钮
【发布时间】:2018-09-06 07:24:22
【问题描述】:

我正在尝试像这样制作一个自定义 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>

【问题讨论】:

  • 这并不完全是一个解决方案,只是一种简单的方法。我认为您可以使用 Tablayout 实现此行为。请参阅 This 。它会像一个 Group 一样工作。试试看。

标签: android android-layout material-design android-radiobutton android-chips


【解决方案1】:

我建议你这个解决方案:

创建两个形状状态:

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

查看结果:

【讨论】:

    【解决方案2】:

    您可以使用Material Components Library 中包含的默认ChipGroup

       <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2019-10-11
      • 2021-04-23
      • 2018-10-08
      • 2015-08-16
      • 2012-07-25
      相关资源
      最近更新 更多