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

Android LinearLayout布局:顶部ScrollView最大占屏50%,底部自适应

布局实现方案

直接使用垂直方向的LinearLayout,结合layout_weight和maxHeight属性即可实现需求,完整布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 顶部ScrollView:内容高度自适应,最大不超过屏幕50% -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxHeight="0dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="这里是顶部ScrollView的内容,高度自适应,当内容高度超过屏幕一半时会被限制在50%高度,否则随内容高度变化"/>

    </ScrollView>

    <!-- 底部ScrollView:至少占屏幕50%,填充剩余空间 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="这里是底部ScrollView的内容,当顶部内容高度小于屏幕一半时,底部会占据剩余的全部空间(超过50%);当顶部内容被限制在50%高度时,底部也会占据50%屏幕高度"/>

    </ScrollView>

</LinearLayout>

关键属性说明

  • 父LinearLayout设置android:orientation="vertical"和android:layout_height="match_parent",确保占满屏幕高度。
  • 顶部ScrollView:
    • android:layout_height="wrap_content":让高度随内部内容自适应。
    • android:layout_weight="1" + android:maxHeight="0dp":利用LinearLayout的权重分配规则,0dp的maxHeight会被解析为父布局高度的1/2(因为两个子View权重都是1),从而限制顶部最大高度为屏幕的50%。
  • 底部ScrollView:
    • android:layout_height="0dp" + android:layout_weight="1":默认会分配到屏幕的50%高度;如果顶部内容高度小于50%,底部会自动填充剩余的全部空间,保证至少占50%屏幕高度。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 22:40:40