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

Android如何将标题与ScrollView纳入带边框布局(续篇)

解决方案:将标题与ScrollView整合到同一带边框布局

你可以直接用一个带边框背景的容器布局把标题区块和ScrollView包裹起来,完全替代原来的额外FrameLayout方案,这样结构更清晰,也符合你的需求。

修改后的完整布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/prop1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <!-- 带边框的容器,直接包裹标题和ScrollView -->
    <LinearLayout
        android:id="@+id/bordered_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/border"
        android:orientation="vertical"
        android:padding="8dp"
        app:layout_constraintBottom_toTopOf="@id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!-- 标题区块 -->
        <LinearLayout
            android:id="@+id/title_block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="100dp"
            android:paddingVertical="8dp">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="16dp"
                android:background="#cccccc"
                android:minHeight="100dp"
                android:text="TITLE" />
        </LinearLayout>

        <!-- ScrollView -->
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="16dp"
            android:layout_weight="1"
            android:paddingBottom="8dp">

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

                <TableRow android:background="#ff9999">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minHeight="100dp"
                        android:text="first row" />
                </TableRow>
                <TableRow android:background="#99ff99">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minHeight="100dp"
                        android:text="second row" />
                </TableRow>
                <TableRow android:background="#bbbbff">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minHeight="100dp"
                        android:text="third row" />
                </TableRow>
            </TableLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/border"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bordered_container"
        app:layout_constraintVertical_bias="0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="validate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关键修改说明:

  • 移除了原来用于模拟边框的extra_layout FrameLayout,改用一个直接包裹标题和ScrollView的LinearLayout作为容器,给它设置@drawable/border背景实现边框效果
  • 给容器LinearLayout设置android:orientation="vertical",让标题和ScrollView垂直排列
  • 给ScrollView设置android:layout_weight="1",确保在容器内剩余空间会被ScrollView填充,同时保持标题的固定高度
  • 调整了容器和其他控件的约束关系,确保整体布局和原效果一致

内容的提问来源于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 14:10:18