我在一个 RadioGroup 中有 4 个 RadioButton,如下所示:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/button1"
android:text="Button 1"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/button2"
android:text="Button 2"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/button3"
android:text="Button 3"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/button4"
android:text="Button 4"/>
</RadioGroup>
</RelativeLayout>
我希望 RadioButton 2 在 RadioButton 1 的右侧,我希望 RadioButton 3 在 RadioButton 1 的下方,我希望 RadioButton 4 在 RadioButton 3 的右侧。
普通属性如
android:layout_below=""
和
android:layout_toRightOf""
不适用于单选按钮。我怎样才能按照上面描述的方式在 XML 中放置 RadioButton?
最佳答案
解决方案 1:使用 LinearLayout
:
- 添加
LinearLayout
作为RadioGroup
的直系子代并给它vertical
方向使用android:orientation="vertical"
. - 再添加两个
LinearLayout
上面里面LinearLayout
与android:orientation="horizontal"
和android:weightSum="2"
. - 输入
button1
和button2
进入第一横LinearLayout
然后放button3
和button4
进入第二横LinearLayout
. - 给出全部 4
buttons
重量1
通过使用属性android:layout_weight="1"
.
试试这个:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<RadioButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 3"/>
<RadioButton
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 4"/>
</LinearLayout>
</LinearLayout>
</RadioGroup>
</RelativeLayout>
输出:
解决方案 2:使用 RelativeLayout
:
- 添加
RelativeLayout
作为RadioGroup
的直系子代并把所有RadioButton
在这里面RelativeLayout
. - 添加属性
android:layout_toRightOf="@id/button1"
至button2
显示它right
的button1
. - 添加属性
android:layout_below="@id/button1
"到button3
以显示below
的button1
。 - 添加属性
android:layout_toRightOf="@id/button3"
和android:layout_alignBottom="@id/button3"
至button4
显示它的权利button3
.
尝试一下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<RadioButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:text="Button 2"/>
<RadioButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="Button 3"/>
<RadioButton
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button3"
android:layout_alignBottom="@id/button3"
android:text="Button 4"/>
</RelativeLayout>
</RadioGroup>
</RelativeLayout>
输出:
仅供引用,您可以使用 padding
或 margin
在 buttons
之间根据您的需要。
希望对你有帮助~
关于android - 我如何安排 RadioButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559222/