android - 我如何安排 RadioButtons?

标签 android xml attributes radio-button radio-group

我在一个 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 :

  1. 添加LinearLayout作为 RadioGroup 的直系子代并给它vertical方向使用 android:orientation="vertical" .
  2. 再添加两个LinearLayout上面里面LinearLayoutandroid:orientation="horizontal"android:weightSum="2" .
  3. 输入button1button2进入第一横LinearLayout然后放button3button4进入第二横LinearLayout .
  4. 给出全部 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>

输出:

enter image description here

解决方案 2:使用 RelativeLayout :

  1. 添加RelativeLayout作为 RadioGroup 的直系子代并把所有 RadioButton在这里面 RelativeLayout .
  2. 添加属性 android:layout_toRightOf="@id/button1"button2显示它rightbutton1 .
  3. 添加属性 android:layout_below="@id/button1 "到 button3 以显示 belowbutton1
  4. 添加属性 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>

输出:

enter image description here

仅供引用,您可以使用 paddingmarginbuttons 之间根据您的需要。

希望对你有帮助~

关于android - 我如何安排 RadioButtons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559222/

相关文章:

android studio Logcat在单行和部分数据中显示json数据

android - AIDE 和 Eclipse 项目错误

attributes - OpenERP 字段属性 - TreeView 中的不可见属性

python - 使用实例属性中的关键字的方法

横向的Android水平recyclerview填充屏幕

android - 查询 ContentProvider 的某些 ID

android - 使用 ScrollView(和 NestedScrollView)时 CollapsingToolbarLayout 不折叠

c# - 如何获取没有子元素的元素?

xml - odoo/openERP中子节点的Qweb Xpath?

html - 非标准属性有什么用?