android - 具有相同高度的子视图,具有最大高度的视图
问题描述
我正在尝试将在 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>
也许有一个使用设计器的解决方案。如果不可能,我想我可能必须通过找到最大高度然后将高度分配给其他视图来以编程方式进行(如果你知道怎么做,我也会接受它作为有效答案)。
更新:
我画了两张图片来说明我的问题。灰色区域是父线性布局。孩子们线性布局的绿色区域。红色区域是上面布局中注释的“其他视图”。
解决方案
通过以下调整,我实现了类似于您的第二张图片的布局
<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>
推荐阅读
- xcode - Xcode 13 AppleScript 框架无法将 UI 连接到 Applescript 代码
- odoo - 如何通过不同模型中的创建功能自动将新记录添加到 One2Many 字段?
- python - Python xml.etree.ElementTree.ParseError 从 XML 转换为 CSV
- javascript - 使用网格布局将填充调整到中心页面
- gitlab - 如何从 CI_PROJECT_DIR 获取配置文件以在服务命令中使用并从服务获取数据作为工件
- mongodb - 如何在 MongoDB 中使用两个字段进行双组和推送?
- java - Postgres 更新查询导致锁定等待飙升
- haskell - 如何在 Haskell 中编写代码以从列表中过滤掉 Nothing 值
- openstack - 策略不允许执行 os_compute_api:os-flavor-manage:create。(HTTP 403)
- c# - nodejs Net socket客户端与c#.net服务器应用程序通信