如何将一个布局的高度设置为另一个布局?

时间:2018-03-26 07:40:31

标签: android android-layout android-linearlayout

我有一个父LinearLayout,其中有两个孩子LinearLayout。我在第一个子布局中有4个TextView,在第二个子布局中有2个TextView。父布局也具有属性android:layout_height="wrap_content以及子布局。

我想将第一个子布局的高度设置为第二个子布局,因为第一个子布局有4个文本框,第二个子布局有文本框。

我只想调整第二个子布局的高度。我把我的代码放在下面。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal"
    android:layout_alignParentTop="true">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".8"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lblBooking_Code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lblSession"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lblCustomer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lblAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lblMobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lblBooking_Gross_Amt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lblBooking_PayAmt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lblBooking_Balance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

制作第二个子布局的高度"match_parent"

答案 1 :(得分:0)

使第二个子布局的高度“match_parent”并实现“weightSum = 3”我达到了我的要求。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:orientation="horizontal"
    android:layout_alignParentTop="true">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".8"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lblBooking_Code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lblSession"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/lblCustomer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lblAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lblMobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight=".1"
    android:weightSum="3"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lblBooking_Gross_Amt"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lblBooking_PayAmt"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/lblBooking_Balance"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

</LinearLayout>
相关问题