简体   繁体   English

创建自定义单选按钮

[英]Create custom radio button

I am trying to create the attached image using radio button groups.我正在尝试使用单选按钮组创建附加图像。 After trying multiple combinations I am not able to achieve what is intended.尝试多种组合后,我无法实现预期的效果。 The intention is to create a circular background around the solid circle whenever one radio button is selected.目的是在选择一个单选按钮时围绕实心圆创建一个圆形背景。

在此处输入图片说明

Any help would be appreciated.任何帮助,将不胜感激。

The radio button images are controlled by a StateListDrawable .单选按钮图像由StateListDrawable控制。 When a radio button is check (state_checked="true"), the drawable assigned to the checked state is shown. When a radio button is check (state_checked="true"), the drawable assigned to the checked state is shown. When the state is "not checked" (state_checked="false"), the drawable assigned to the "not checked" state is shown.当状态为“未选中”(state_checked="false")时,会显示分配给“未选中”状态的可绘制对象。 Here is an example StateListDrawable :这是一个示例StateListDrawable

custom_radio_button.xml custom_radio_button.xml

<selector>
    <item android:state_checked="true" android:drawable="@drawable/ic_baseline_check_circle_24" />
    <item android:state_checked="false" android:drawable="@drawable/ic_baseline_check_circle_outline_24" />
</selector>

Here, I have just selected two vector graphic files from Android Studio.在这里,我刚刚从 Android Studio 中选择了两个矢量图形文件。 You will need to create the checked/unchecked images that you want.您将需要创建所需的选中/未选中的图像。

Now, in the XML for the layout, specify the you want to use this StateListDrawable for the radio button.现在,在布局的 XML 中,指定要将此StateListDrawable用于单选按钮。

...some XML...
<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_radio_button"
    android:checked="true"
    android:text="RadioButton 1" />
...more XML...

You would need to place this radio button and its sisters into a radio group.您需要将这个单选按钮和它的姐妹放在一个单选组中。

Putting it all together gives the following:将它们放在一起给出以下内容:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM