我有一个在 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是嵌套的directlyRadioGroup 下只能选择一个。有没有一种方法可以包装我的单选按钮,但又不能同时选择多个单选按钮?
如果没有,有没有更好的方法来完成我想要的造型?
使用完整的 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
and/or margin
或者进行其他调整以达到您想要的效果,但我认为这会让您接近。如果我误解了请告诉我
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)