You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android单个Activity中实现3个布局各占1/3垂直高度

垂直LinearLayout等分高度嵌套多布局实现

要实现三个子布局各占总高度1/3的效果,利用LinearLayout的权重分配特性即可完成,实现方式如下:

  • 给外层垂直LinearLayout设置android:weightSum="3",声明总权重为3
  • 三个子布局统一配置android:layout_height="0dp" + android:layout_weight="1",即可各自分配1/3的总高度

完整的布局示例代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">

    <!-- 第一个ConstraintLayout子布局 -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 内部自定义内容 -->
    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- RelativeLayout子布局 -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 内部自定义内容 -->
    </RelativeLayout>

    <!-- 第二个ConstraintLayout子布局 -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 内部自定义内容 -->
    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

注意:如果你使用的是非AndroidX的老版本支持库,把ConstraintLayout的类路径替换为android.support.constraint.ConstraintLayout即可正常使用。

内容的提问来源于stack exchange,提问作者yoan_gauthier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 22:06:03