简体   繁体   中英

how to set Linear layout child's height as tallest child height

I have four textviews in my Linear Layout and they have equal width as follows,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView86" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.25"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.25"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.25"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.25"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

My problem is, The textviews should be displayed with the same height.The content(text) for that textview will be set programmatically. I tried with Relative layout but the width of textview couldn't be same. I need both height and width should be same as tallest child .

Guide me please. Thanks!

Use below layout :

<LinearLayout
    android:weightSum="1"
    android:background="@color/colorPrimaryDark"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:background="@color/black_30"
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="match_parent"
        />
    <TextView
        android:background="@color/com_facebook_blue"
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="match_parent"
        />
    <TextView
        android:background="@color/tw__composer_red"
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="match_parent"
        />
    <TextView
        android:background="@color/colorAccent"
        android:layout_width="0dp"
        android:layout_weight="0.25"
        android:layout_height="match_parent"
        />
</LinearLayout>

Checkout below xml Code , You set the child height to wrap_content that should be match_parent :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:background="@drawable/bluebackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView86" />
    <LinearLayout
        android:background="@color/colorPrimaryDark"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:background="@drawable/bluebackground"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            />
        <TextView
            android:background="@drawable/bluebackground"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            />
        <TextView
            android:background="@drawable/bluebackground"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            />
        <TextView
            android:background="@drawable/bluebackground"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            />
    </LinearLayout>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM