Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何排列单选按钮?_Android_Xml_Attributes_Radio Button_Radio Group - Fatal编程技术网

Android 如何排列单选按钮?

Android 如何排列单选按钮?,android,xml,attributes,radio-button,radio-group,Android,Xml,Attributes,Radio Button,Radio Group,我在一个放射组中有4个单选按钮,如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content"> &l

我在一个放射组中有4个单选按钮,如下所示:

<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>


不要为单选按钮工作。如何在此处以XML中上述方式放置单选按钮?

我建议您不要通过在放射组中使用嵌套布局(线性/相对)来增加视图层次结构。此外,使用嵌套布局也不会获得单个选择功能。RadioGroup实际上扩展了LinearLayout。因此,它只能垂直或水平排列单选按钮。这里我分享了链接
我的库的一部分,它实际上是一个相对论组,因此您可以根据需要排列单选按钮。

尝试以下代码

    <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

          <LinearLayout
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:oriendtation="horizontal"/>

  <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"/>


 </LinearLayout>



    <LinearLayout
             android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:oriendtation="horizontal"/>


        <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"/>


 </LinearLayout>


我希望这可能会对您有所帮助。

解决方案1:使用
LinearLayout

<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>
<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>
  • 添加
    LinearLayout
    作为
    RadioGroup
    的直接子对象,并使用
    android:orientation=“vertical”
    为其提供
    vertical
    方向
  • 使用
    android:orientation=“horizontal”
    android:weightSum=“2”
    在上面的
    LinearLayout
    内部添加另外两个
    LinearLayout
  • 按钮1
    按钮2
    放入第一个水平
    线性布局
    ,并将
    按钮3
    按钮4
    放入第二个水平
    线性布局
  • 使用属性
    android:layout\u weight=“1”
    为所有4个
    按钮赋予权重
    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>
    
    <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>
    
    输出:

    <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>
    
    <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>
    

    仅供参考,您可以根据需要在
    按钮之间使用
    填充
    边距

    希望这会有所帮助~

    你可以试试这个

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:ignore="NewApi">
    
    
    
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button1"
                android:text="Button 1"/>
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button2"
                android:text="Button 2"
                android:layout_toRightOf="@+id/button1"/>
        </RelativeLayout>
    
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button3"
                android:text="Button 3"/>
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button4"
                android:text="Button 4"
                android:layout_toRightOf="@+id/button3"/>
        </RelativeLayout>
    
    </RadioGroup>
    
    
    

    检查下面我的答案。我添加了一个带有输出的工作代码。希望这将有助于如果我的答案似乎有用,那么请放弃投票。谢谢你接受我的回答。如果我的答案似乎有用,那么请投赞成票。现在的布局就像我想要的那样,非常有用!但是我现在有一个很大的副作用。每个按钮都可以一起选择,现在都打开了。我怎样才能解决这个问题?(我尝试了线性布局的方式。)如果你可以将这个问题作为不同的问题发布,那就更好了。我会尝试的!如果你有答案,到这里来:我知道问题会发生。所以我开发了一个图书馆,它实际上是一个相对论群体。我在回答和你的问题中都给出了细节。