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

Android中ScrollView内TableLayout显示被裁剪问题求助

问题描述

在ScrollView中嵌套TableLayout后,TableLayout内容显示被裁剪。相关布局和代码如下:

嵌套ScrollView与TableLayout的布局代码

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_editor_absoluteX="0dp">

        <TableLayout
            android:id="@+id/notesTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </TableLayout>
    </ScrollView>

动态添加元素的Kotlin代码

notesList.forEach{

    val noteBodyText = sp.getString(it, "Empty Body")!!

    /* Find Tablelayout defined in main.xml */
    val tl = findViewById<View>(R.id.notesTable) as TableLayout
    /* Create a new row to be added. */
    val tr = TableRow(this)
    tr.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT,
        TableRow.LayoutParams.WRAP_CONTENT
    )
    //note title
    val noteTitle = TextView(this)
    noteTitle.text = it
    noteTitle.setTextColor(getColor(R.color.black))
    noteTitle.textSize = 22F
    noteTitle.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT,
        30
    )
    tr.addView(noteTitle)

    val noteBody = TextView(this)
    noteTitle.setTextColor(getColor(R.color.dark_gray))
    noteTitle.textSize = 15F
    noteBody.text = noteBodyText
    noteBody.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT,
        20
    )
    tr.addView(noteBody)

    tl.addView(
        tr,
        TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT
        )
    )

}

完整布局文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginBottom="-90dp"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_yellow_wide_rectangle"/>

    <Button
        android:id="@+id/backHomeButtonRead"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginEnd="8dp"
        app:icon="@drawable/ic_baseline_home_purple"
        app:iconPadding="4dp"
        app:iconSize="18dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="64dp"
        android:text="Your notes:"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="@+id/backHomeButtonRead"
        app:layout_constraintStart_toStartOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_editor_absoluteX="0dp">

        <TableLayout
            android:id="@+id/notesTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </TableLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
解决方法

1. 清理TableLayout的无效属性

TableLayout是ScrollView的直接子View,不属于ConstraintLayout的直接子View,因此app:layout_constraintBottom_toBottomOf和app:layout_constraintTop_toTopOf这两个约束属性完全无效,反而会干扰布局计算。同时关闭android:isScrollContainer,因为ScrollView已经承担了滚动容器的角色。

修改后的TableLayout代码:

<TableLayout
    android:id="@+id/notesTable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"> <!-- 可选:添加内边距优化显示 -->
</TableLayout>

2. 修正ScrollView的布局约束

当前ScrollView的约束设置会导致布局高度计算错误,应该让它的顶部对齐标题TextView的底部,同时用0dp让约束控制高度,避免内容被截断:

修改后的ScrollView代码:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="16dp"
    android:scrollbars="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2">

3. 修复动态添加View的代码错误

代码中存在两个问题:一是误将noteBody的样式属性应用到了noteTitle上;二是固定高度的布局参数会导致内容被裁剪,改为wrap_content即可:

notesList.forEach{
    val noteBodyText = sp.getString(it, "Empty Body")!!
    val tl = findViewById<TableLayout>(R.id.notesTable)
    val tr = TableRow(this)
    tr.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT
    )
    tr.setPadding(0, 16, 0, 16) // 添加行间距优化显示

    // 标题TextView
    val noteTitle = TextView(this)
    noteTitle.text = it
    noteTitle.setTextColor(getColor(R.color.black))
    noteTitle.textSize = 22F
    noteTitle.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT
    )
    tr.addView(noteTitle)

    // 正文TextView
    val noteBody = TextView(this)
    noteBody.setTextColor(getColor(R.color.dark_gray)) // 修正为设置noteBody的属性
    noteBody.textSize = 15F
    noteBody.text = noteBodyText
    noteBody.layoutParams = TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.WRAP_CONTENT
    )
    noteBody.setPadding(0, 8, 0, 0) // 标题和正文之间添加间距
    tr.addView(noteBody)

    tl.addView(tr)
}

4. 调整ImageView的负边距(可选)

ImageView设置的android:layout_marginBottom="-90dp"可能会覆盖ScrollView的顶部内容,如果不需要这个视觉效果,建议移除或调整该边距值,避免布局重叠。

内容的提问来源于stack exchange,提问作者nicolello.dev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 08:24:13