问题描述
我试图像这样制作一个自定义RadioButton:
I'm trying to make a custom RadioButton like this:
我尝试制作一个带有三个RadioButton的RadioGroup,其中RadioGroup的背景为矩形,如果选中,RadioButton的背景将为蓝色矩形,否则为白色,但似乎不起作用. /p>
I tried making a RadioGroup with three RadioButton in it with the background of the RadioGroup being a rectangle and the background of the RadioButton would be a blue rectangle when checked and white when not but it doesn't seem to be working.
<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>
推荐答案
我建议您使用此解决方案:
I suggest you this solution :
创建两个形状状态:
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>
然后定义文本和背景颜色的选择器.
Then define selector for both text and background color.
在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>
最后,添加背景图选择器和文本选择器:
Finally, add backgroud selectr and text 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" />
查看结果:
这篇关于制作自定义单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!