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

梦想与她 提交于 2020-05-29 10:38:08

问题


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!


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/47305334/how-to-set-linear-layout-childs-height-as-tallest-child-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!