【发布时间】:2013-06-14 00:11:43
【问题描述】:
我在 XML 中定义了一个 RadioGroup,它有两个 RadioButton。但是,我需要将标签显示在按钮本身的左侧,标签左对齐,按钮右对齐。为此,我使用了一个包含 TextView 和 RadioButton 的 RelativeLayout,没有 text。这大概是布局的样子:
<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 但同时防止多个单选按钮被选中?
如果没有,有没有更好的方法来完成我所追求的样式?
【问题讨论】:
-
@MaciejGórski 这个问题非常接近,尽管它只需要在按钮和标签之间进行一些填充,而不是让它们与相对侧对齐。我相信对齐它们需要在 RadioGroup 和 RadioButton 之间有一个 ViewGroup(这可以防止选择相互排斥),但我很好奇可能有哪些替代方案,如果有的话。
标签: android