在水平方向的Radiogroup中管理布局

18

我正在使用TableLayout和TableRows作为我的主要活动窗口。

在TableLayout中有一个包含两个单选按钮的单选组(单选组位于表行内)。我想将最右边的单选按钮与屏幕右边缘对齐,使"间隙"位于左单选按钮和右单选按钮之间而不是右单选按钮后面。 即

所以,不是这样
| (x) (x) 间隙 |
我会这样
|(x) 间隙 (x)|

其中(x)表示单选按钮,|表示屏幕的边缘

我可以使用gravity(center_horizontal)将两个按钮放在中间(即| 间隙 (x)(x) 间隙 |),但是我似乎无法像之前所说的那样将它们分开。

4个回答

35

要让任意数量的按钮水平均匀地分布在屏幕上,您需要:

  1. RadioGroup必须设置为 android:orientation="horizontal"android:layout_width="fill_parent"
  2. 每个单选按钮必须设置为 android:layout_weight="1"(除了最右边的按钮,它需要靠右对齐)

横向截图

我花了几个小时才弄明白这个问题。

以下是一个示例代码,包括两个文本标签和屏幕的左右边缘,用于调查应用。

<RadioGroup
    android:id="@+id/radio_group"
    android:orientation="horizontal"
    android:layout_below="@id/question" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
>
    <RadioButton
        android:id="@+id/strong_disagree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"
    />
    <RadioButton
        android:id="@+id/disagree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/disagree"
    />
    <RadioButton
        android:id="@+id/neutral_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/neutral"
    />
    <RadioButton
        android:id="@+id/agree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/agree"
    />
    <RadioButton
        android:id="@+id/strong_agree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
    />
</RadioGroup>
<TextView
    android:id="@+id/disagree_label"
    android:text="@string/strongly_disagree_txt"
    android:layout_below="@id/radio_group" 
    style="@style/TextAppearance"
    android:visibility="gone"
    />
<TextView
    android:id="@+id/agree_label"
    android:text="@string/strongly_agree_txt"
    android:layout_below="@id/radio_group" 
    android:layout_alignParentRight="true" 
    style="@style/TextAppearance"
    android:layout_width="wrap_content"
    android:visibility="gone"
    />

1
我尝试了这个,但它和你附加的截图不一样。 - KarenAnne
1
非常感谢您,我已经快被这个问题搞疯了! - Garbit

9

我找到了解决方案。通过权重和重力的结合以及去除边距,可以实现。单选按钮组可以设置layout_width="fill_parent"。每个单选按钮应该具有layout_weight="1",但是每个单选按钮的layout_width仍必须指定以覆盖默认值"37",我猜这是默认的单选按钮图像宽度。

        <RadioGroup android:id="@+id/overall"
        android:layout_width="fill_parent" android:layout_height="80dp"
        android:gravity="center"
        android:orientation="horizontal">

第一个按钮应该有:

                android:layout_weight="1"
            android:layout_gravity="center|left"

最后一个按钮应该具备以下功能:

            android:layout_gravity="center|right"

注意:所有按钮都设置了layout_gravity,但最右边的按钮没有设置layout_weight!

我使用了那个,但它对我的代码没有用,第一个按钮覆盖了整行。 - elifekiz

0
你有找到解决方案吗?我的中间解决方案可能会有所帮助,但对我来说并不是完整的解决方案。我已经通过设置特定的宽度来实现这一点,但这并不允许视图调整大小以适应屏幕宽度:
    <RelativeLayout  
    android:id="@+id/overall_layout"
    android:layout_width="290dp" android:layout_height="wrap_content">

    <RadioGroup android:id="@+id/overall"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton android:id="@+id/overall_1" android:tag="1"
            android:button="@drawable/radio_1"
            android:layout_width="50dp"
            android:layout_marginRight="10dp"
            android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:id="@+id/overall_2" android:tag="2"
            android:button="@drawable/radio_2"
            android:layout_width="50dp"
            android:layout_marginRight="10dp"
            android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:id="@+id/overall_3" android:tag="3"
            android:button="@drawable/radio_3"
            android:layout_width="50dp"
            android:layout_marginRight="10dp"
            android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:id="@+id/overall_4" android:tag="4"
            android:button="@drawable/radio_4"
            android:layout_width="50dp"
            android:layout_marginRight="10dp"
            android:layout_height="wrap_content"></RadioButton>
        <RadioButton android:id="@+id/overall_5" android:tag="5"
            android:button="@drawable/radio_5"
            android:layout_width="50dp"
            android:layout_height="wrap_content"></RadioButton>
    </RadioGroup>
    <TextView android:text="left" android:id="@+id/left"
        android:layout_below="@id/overall"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="right" android:id="@+id/right"
        android:layout_below="@id/overall"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </RelativeLayout>

请注意,最后一个按钮没有边距。

我正在尝试在单选组中放置5个自定义按钮,第一个左对齐,最后一个右对齐。按钮的宽度恰好为50像素,我不希望任何文本与按钮单独关联。下面的2个文本在相对布局中分别左对齐和右对齐。使用“layout_gravity”没有效果,“layout_weight”会在每个按钮的右侧添加填充,这会导致最右侧的按钮不再对齐。

这让我很苦恼。


0

如果我理解你的问题正确,你也可以尝试在RadioGroup中使用下面的代码:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    android:id="@+id/view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接