RadioGroup在垂直LinearLayout中占用了太多空间

时间:2015-09-14 20:06:56

标签: android xml android-layout android-studio

我有一个垂直线性布局的活动。屏幕的顶部一半应该是纯文本视图,而底部一半应该是一个包含四个单选按钮的RadioGroup。

我将TextView和RadioGroup设置为android:layout_weight="1"

在我将按钮放入RadioGroup之前,布局很好,因为RadioGroup和TextView都占据了屏幕的一半。但是在拖动单选按钮并将它们全部设置为android:layout_weight="1"后,带有四个按钮的RadioGroup现在占据了屏幕的一半以上。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"


<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sample Question Text"
        android:layout_weight="1"
        android:id="@+id/textView2"
        android:gravity="center"
        android:textSize="24dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:layout_gravity="center_horizontal">

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sample Choice #1"
                android:id="@+id/radioButton"
                android:layout_weight="1"
                android:layout_gravity="center_horizontal"
                android:checked="false" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sample Choice #2"
                android:id="@+id/radioButton2"
                android:layout_weight="1"
                android:layout_gravity="center_horizontal"
                android:checked="false" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sample Choice #3"
                android:id="@+id/radioButton3"
                android:layout_weight="1"
                android:layout_gravity="center_horizontal"
                android:checked="false" />

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sample Choice #4"
                android:id="@+id/radioButton4"
                android:layout_weight="1"
                android:layout_gravity="center_horizontal"
                android:checked="false" />
        </LinearLayout>
    </RadioGroup>

</LinearLayout>

如果我只放置2个或3个按钮

,也会出现此问题

1 个答案:

答案 0 :(得分:1)

您需要将TextView和RadioGroup设置为android:layout_height="0dp" 当您使用重量时,您将根据您提供的重量指示计算高度或宽度(取决于它是垂直还是水平布局)。在这种情况下,您还会根据&#39; wrap_content&#39;提供计算高度的说明,这些冲突的说明会导致问题。

您的RadioButtons也是如此。无论何时使用layout_weight,您的高度或宽度应为0dp。

相关问题