首页 > 解决方案 > 具有相同高度的子视图,具有最大高度的视图

问题描述

我正在尝试将在 ios 上非常容易的东西复制到 android。

我想要在运行时确定高度的相同高度的视图。视图的高度应基于高度最大的视图。

我发现了一个类似的问题(链接),但它基于按钮可能是我的解决方案不起作用的原因。

例子:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    <!--Other views-->
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    <!--Other views-->
    </LinearLayout>

</LinearLayout>

也许有一个使用设计器的解决方案。如果不可能,我想我可能必须通过找到最大高度然后将高度分配给其他视图来以编程方式进行(如果你知道怎么做,我也会接受它作为有效答案)。

更新:

我画了两张图片来说明我的问题。灰色区域是父线性布局。孩子们线性布局的绿色区域。红色区域是上面布局中注释的“其他视图”。

实际结果: 在此处输入图像描述

预期结果: 在此处输入图像描述

标签: androidviewheight

解决方案


通过以下调整,我实现了类似于您的第二张图片的布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_margin="20dp"
    android:background="@color/colorAccent">
    <!--Other views-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_margin="20dp"
    android:background="@color/colorAccent">
    <!--Other views-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>


推荐阅读