【问题标题】:How do I arrange RadioButtons?如何安排 RadioButtons?
【发布时间】:2017-09-19 10:20:40
【问题描述】:

我在 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 中描述的方式放置 RadioButtons?

【问题讨论】:

  • 在下面查看我的答案。我添加了一个带输出的工作代码。希望这会有所帮助
  • 如果我的回答似乎有用,请投赞成票。提前致谢

标签: android xml attributes radio-button radio-group


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试下面的代码,

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

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      解决方案 1:使用LinearLayout

      1. LinearLayout 添加为RadioGroup 的直接子代,并使用android:orientation="vertical" 为其指定vertical 方向。
      2. LinearLayout 上方添加另外两个LinearLayoutandroid:orientation="horizontal"android:weightSum="2"
      3. button1button2 放入第一个水平LinearLayout 并将button3button4 放入第二个水平LinearLayout
      4. 使用属性android:layout_weight="1" 赋予所有4 个buttons 权重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

      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>
      

      输出:

      仅供参考,您可以根据需要在buttons 之间使用paddingmargin

      希望对你有帮助~

      【讨论】:

      • 感谢您接受我的回答。如果我的回答似乎有用,请投赞成票。
      • 现在布局就像我想要的那样,非常有用!但我现在有很大的副作用。每个按钮都可以一起选择,现在它们都打开了。我该如何解决这个问题? (我尝试了 LinearLayout 方式。)
      • 如果您可以将此问题作为不同的问题发布,那就更好了。
      • 我会试试的!如果您有答案,请访问:stackoverflow.com/questions/43563150/…
      • 我知道会出现问题。所以我开发了一个库,它实际上是一个RelativeRadioGroup。我在答案和您的stackoverflow.com/questions/43563150/… 问题中提供了详细信息。
      【解决方案4】:

      你可以试试这个

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

      【讨论】:

        猜你喜欢
        • 2012-11-04
        • 2011-02-12
        • 1970-01-01
        • 2010-09-28
        • 2020-10-15
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多