【问题标题】:how to set Linear layout child's height as tallest child height如何将线性布局孩子的高度设置为最高的孩子高度
【发布时间】:2018-04-28 13:20:11
【问题描述】:

我的Linear Layout 中有四个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>

我的问题是,文本视图应该以相同的高度显示。该文本视图的内容(文本)将以编程方式设置。我尝试使用Relative layout,但textview 的width 不能相同。我需要高度和宽度都应与最高的孩子相同

请指导我。谢谢!

【问题讨论】:

  • 为您的布局提供完整代码。您可以在父布局中使用 layout_weight 来获取结果。
  • 你想让 textxtview 水平对齐还是垂直对齐?
  • 就像,在一行中,它们应该以相同的高度和宽度显示。

标签: android android-layout


【解决方案1】:

使用下面的布局:

<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”即可。 :)
  • 这就是 LinearLayout 对子视图的反应。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-02-04
    • 2016-10-22
    • 2018-10-24
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多