如何在Android中的父RelativeLayout中嵌套LinearLayout?

时间:2014-06-03 13:47:37

标签: android android-layout android-fragments

我正在尝试制作一个示例计算器应用程序,其中我需要在父RelativeLayout中嵌套多个LinearLayout,每个LinearLayout包含2个水平对齐的文本框和一个用于选择操作的单选按钮,所以我有单选按钮用于加法,减法,乘法和除法的运算。 UI应该看起来像图像中的那个,其中我有2个文本字段和一个水平对齐的单选按钮,但是我无法想出一种有效的方法来在RelativeLayout中有效地嵌套LinearLayouts,一些帮助将不胜感激。

以下是描述应用程序UI的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout 
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
        <EditText android:id="@+id/edit_number1" 
                  android:inputType="number"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"/>

        <EditText android:id="@+id/edit_number2" 
                  android:inputType="number"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"/>

        <RadioButton
            android:id="@+id/addition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/addition" 
            android:checked="false" />
    </LinearLayout>

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:layout_below="@+id/layout1">
        <EditText android:id="@+id/edit_number3" 
                  android:inputType="number"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"/>

        <EditText android:id="@+id/edit_number4" 
                  android:inputType="number"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"/>

        <RadioButton
            android:id="@+id/subtraction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/subtraction" 
            android:checked="false" />
    </LinearLayout>

</RelativeLayout>

enter image description here

1 个答案:

答案 0 :(得分:2)

将每个layout_height上的LinearLayout更改为wrap_content