如何将线性布局子项的高度设置为最高子项的高度

4

我在我的线性布局中有四个TextViews,它们的宽度相等,如下所示:

<?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>

我的问题是,TextView应该显示相同的高度。文本内容将通过编程方式设置。我尝试使用RelativeLayout,但TextView的宽度不能相同。我需要最高子项的高度和宽度都相同。请指导我。谢谢!

请提供您的完整布局代码。您可以在父布局中使用layout_weight来获得结果。 - Piash Sarker
你想让textview水平对齐还是垂直对齐? - ADM
在一行中,它们应该具有相同的高度和宽度。 - Jyoti JK
2个回答

6
请使用以下布局:

<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>

谢谢!只需将高度更改为“match_parent”即可。 :) - Jyoti JK
这就是LinearLayout对子视图的反应方式。 - ADM

0
请查看下面的XML代码,你将子项高度设置为wrap_content,但应该是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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接