【问题标题】:How to set height of one layout to another layout?如何将一种布局的高度设置为另一种布局?
【发布时间】:2018-03-26 07:40:30
【问题描述】:

我有一个父母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>

【问题讨论】:

  • 给出第二个布局 android:layout_height="match_parent"
  • 你想要什么post your screenshot here ?
  • @VishalYadav,我解决了。

标签: android android-layout android-linearlayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使第二个子布局的高度为“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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      相关资源
      最近更新 更多