我有一个用 XML 定义的 RadioGroup,它有两个 RadioButton。但是,我需要将标签显示在按钮本身的左侧,标签左对齐,按钮右对齐。为此,我使用了一个包含 TextView 和一个没有 text
的 RadioButton 的 RelativeLayout。这大概是布局的样子:
<RadioGroup android:id="@+id/foo" android:layout_height="wrap_content" android:layout_width="match_parent">
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:text="@string/bar_one" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_width="wrap_content" />
<RadioButton android:id="@+id/bar1" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
...
</RadioGroup>
这显示了我期望的方式,但我发现 RadioGroup 允许选择多个 RadioButton。当我删除 RelativeLayouts 和 TextViews 时,RadioButtons 直接嵌套在 RadioGroup 下,只能选择一个。有没有一种方法可以包装我的 RadioButtons 但同时防止多个 RadioButtons 被选中?
如果没有,是否有更好的方法来完成我想要的样式?
最佳答案
使用完整的 xml 会更容易分辨,因为您只显示一个 RadioButton
,因此无法知道另一个的属性是什么,或者屏幕截图会有帮助。但是你可以使用 LinearLayout
并做类似的事情
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
<RadioGroup
...>
<TextView
.../>
<RadioButton
.../>
<TextView
.../>
<RadioButton
.../>
</RadioGroup>
</LinearLayout
但在再次阅读问题后,我认为您可能想要类似的东西
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<RadioGroup
android:id="@+id/foo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true" >
<RadioButton android:id="@+id/bar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/bar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView android:id="@+id/textView1"
android:text="Text 1"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignBottom="@+id/bar1"/>
<TextView android:text="Text 2"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignBottom="@+id/foo" />
</RelativeLayout>
您可能需要调整一些 padding
和/或 margin
或进行其他调整以使其达到您想要的效果,但我认为这会让您接近。如果我误解了请告诉我
关于android - RadioGroup 允许选择多个 RadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17157705/