【问题标题】:Getting a radiogroup to extend evenly horizontally with match_parent使用 match_parent 使 radiogroup 水平均匀扩展
【发布时间】:2016-10-17 07:04:23
【问题描述】:

我遇到的问题是我似乎无法让它正确地匹配_parent。我想要它做的是延伸到页面的末尾,如下图所示。 我几乎尝试了所有方法,但我想出的唯一解决方案是添加一个固定的像素,这是我不想做的事情,因为它在不同平台之间发生冲突。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.battleplugins.msutton.fragemento.MainActivity">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/menu_row">

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:orientation='horizontal'>

                <RadioButton
                    android:text="@string/quiz_button_title"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/quiz_button"
                    android:button="@android:color/transparent"
                    android:background="@drawable/buttonbackground"
                    android:layout_weight="1"
                    android:onClick="updateFragment"/>

                <RadioButton
                    android:text="@string/picture_button_title"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/picture_button"
                    android:button="@android:color/transparent"
                    android:background="@drawable/buttonbackground"
                    android:layout_weight="1"
                    android:onClick="updateFragment"/>


                <RadioButton
                    android:text="@string/events_button_title"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/events_button"
                    android:button="@android:color/transparent"
                    android:background="@drawable/buttonbackground"
                    android:layout_weight="1"
                    android:onClick="updateFragment"/>
            </RadioGroup>
        </TableRow>
    </TableLayout>

</RelativeLayout>

我得到的是这样的:

我想要它做的是:

问题在于这是一个固定的金额。 RadioGroup中的android:layout_width="1075px"

【问题讨论】:

    标签: android android-layout android-studio


    【解决方案1】:

    使用权重,而不是设置绝对宽度。

        <RadioGroup
                    android:weightSum="3"
                    android:layout_width="match_parent"
                    android:layout_height="75dp"
                    android:orientation='horizontal'>
                    <RadioButton
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
    
                    <RadioButton
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
    
    
                    <RadioButton
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
    </RadioGroup>
    

    【讨论】:

      【解决方案2】:

      您可以改用 LinearLayout:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:orientation="horizontal"
          tools:context="net.battleplugins.msutton.fragemento.MainActivity">
                  <RadioGroup
                      android:layout_width="match_parent"
                      android:layout_height="75dp"
                      android:orientation='horizontal'>
      
                      <RadioButton
                          android:text="@string/quiz_button_title"
                          android:layout_width="0dp"
                          android:layout_height="wrap_content"
                          android:id="@+id/quiz_button"
                          android:button="@android:color/transparent"
                          android:background="@drawable/buttonbackground"
                          android:layout_weight="1"
                          android:onClick="updateFragment"/>
      
                      <RadioButton
                          android:text="@string/picture_button_title"
                          android:layout_width="0dp"
                          android:layout_height="wrap_content"
                          android:id="@+id/picture_button"
                          android:button="@android:color/transparent"
                          android:background="@drawable/buttonbackground"
                          android:layout_weight="1"
                          android:onClick="updateFragment"/>
      
      
                      <RadioButton
                          android:text="@string/events_button_title"
                          android:layout_width="0dp"
                          android:layout_height="wrap_content"
                          android:id="@+id/events_button"
                          android:button="@android:color/transparent"
                          android:background="@drawable/buttonbackground"
                          android:layout_weight="1"
                          android:onClick="updateFragment"/>
                  </RadioGroup>
      </LinearLayout>
      

      【讨论】:

      • 你可能是神。非常感谢。
      【解决方案3】:

      LinearLayout 是实现这一目标的最佳方法。

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/activity_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          tools:context="net.battleplugins.msutton.fragemento.MainActivity">
          <RadioGroup
              android:layout_width="match_parent"
              android:layout_height="75dp"
              android:orientation='horizontal'>
      
              <RadioButton
                  android:text="@string/quiz_button_title"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/quiz_button"
                  android:button="@android:color/transparent"
                  android:background="@drawable/buttonbackground"
                  android:layout_weight="1"
                  android:onClick="updateFragment"/>
      
              <RadioButton
                  android:text="@string/picture_button_title"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/picture_button"
                  android:button="@android:color/transparent"
                  android:background="@drawable/buttonbackground"
                  android:layout_weight="1"
                  android:onClick="updateFragment"/>
      
      
              <RadioButton
                  android:text="@string/events_button_title"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:id="@+id/events_button"
                  android:button="@android:color/transparent"
                  android:background="@drawable/buttonbackground"
                  android:layout_weight="1"
                  android:onClick="updateFragment"/>
          </RadioGroup>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        如果您想使用TableLayout,请在TableLayout 中指定android:stretchColumns="1",在RadioGroup 中指定android:layout_column="1"

        【讨论】:

          猜你喜欢
          • 2016-03-16
          • 2014-05-29
          • 2012-01-14
          • 2013-04-01
          • 1970-01-01
          • 2013-07-19
          • 2011-08-05
          • 2015-02-09
          • 1970-01-01
          相关资源
          最近更新 更多