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

如何在Android Activity中实现滚动表格紧跟LinearLayout且按钮固定底部

纯XML实现动态Table+固定底部Button布局

直接上可运行的布局代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 中间内容区域:包含可滚动Table和紧跟的LinearLayout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/B2">

        <!-- 可滚动的Table容器 -->
        <ScrollView
            android:id="@+id/S1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scrollbars="vertical">

            <TableLayout
                android:id="@+id/table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <!-- 这里添加你的TableRow -->
                <TableRow>...</TableRow>
                <TableRow>...</TableRow>
            </TableLayout>
        </ScrollView>

        <!-- 始终紧跟Table的LinearLayout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!-- 这里添加你的LinearLayout内容 -->
        </LinearLayout>
    </LinearLayout>

    <!-- 固定在底部的Button -->
    <Button
        android:id="@+id/B2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="validate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

关键修改说明

  1. ScrollView高度调整:将layout_height="wrap_content"改为0dp并设置layout_weight="1",让ScrollView自动占据外层LinearLayout中除下方固定高度LinearLayout外的所有可用空间——Table内容过短时不会拉伸,内容过长时自动滚动,同时保证下方LinearLayout始终可见。
  2. 外层LinearLayout约束:保持layout_height="0dp"并绑定上下边界,确保它仅占据Button上方区域,不会覆盖底部按钮。
  3. Button居中优化:添加左右居中约束,让按钮显示更规整(可根据需求调整)。

解决的核心问题

  • Table内容过长时,ScrollView滚动,下方LinearLayout始终紧跟Table且不消失
  • Table内容较短时,LinearLayout紧贴Table,不会被拉到Button上方
  • 按钮始终固定在页面底部,不受上方内容变化影响

内容的提问来源于stack exchange,提问作者Laurent D.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 21:26:28