我正在尝试制作这样的自定义RadioButton:
我尝试在其中制作一个带有三个RadioButton的RadioGroup,其中RadioGroup的背景为矩形,RadioButton的背景在检查时为蓝色矩形,而在没有时为白色,但它似乎不起作用。< / p>
<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>
答案 0 :(得分: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>
然后为文本和背景颜色定义选择器。
在text_selector.xml
文件夹中创建res/drawable/
:
<?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>
在background_selector.xml
文件夹中创建res/drawable/
:
<?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>
最后,添加backgroud selectr和文本选择器:
<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" />
查看结果:
答案 1 :(得分:1)
您可以使用ChipGroup
中包含的默认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>