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

ScrollView加载大文本显示异常问题求助(附XML代码)

问题描述

我在Fragment中添加了ScrollView,但加载NewsApi接口返回的大文本时出现显示异常,会显示大量异常/重复字符。我曾怀疑是ConstraintLayout的问题,改用LinearLayout布局后仍未找到问题根源。

以下是我的XML布局代码:

<?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"
    android:background="#ECF0F5"
    tools:context=".ui.Fragments.DescriptionFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        >

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

            <ImageView
                android:id="@+id/return_from_des"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="26dp"
                android:layout_marginTop="37dp"
                android:clickable="true"
                android:src="@drawable/ic_baseline_arrow_back_24"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="100dp"
                android:layout_marginTop="34dp"
                android:layout_marginEnd="100dp"
                android:fontFamily="@font/roboto_bold"
                android:text="Breaking News"
                android:textColor="#003C68"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/desc_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="24dp"
                android:layout_marginRight="16dp"
                android:fontFamily="@font/roboto_bold"
                android:text="Apple's 2021 App Store Awards highlight 'connection'"
                android:textColor="#000000"
                android:textSize="25sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView" />

            <TextView
                android:id="@+id/desc_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="15dp"
                android:layout_marginRight="16dp"
                android:fontFamily="@font/roboto"
                android:text="In this article: LumaFusion, Craft, news, gear, Carrot Weather, Space Marshals 3, gaming, Dazn, League of Legends: Wild Rift, Myst, Apple, Among Us, Canva, Toca Life World, Penaut, MARVEL Future Revolution, App Store Awards. Bumble, FantasianIn this article: LumaFusion, Craft, news, gear, Carrot Weather, Space Marshals 3, gaming, Dazn, League of Legends: Wild Rift, Myst, Apple, Among Us, Canva, Toca Life World, Penaut, MARVEL Future Revolution, App Store Awards. Bumble, Fantasian"
                android:textColor="#A6A7A7"
                android:textSize="16sp"
                app:layout_constraintTop_toBottomOf="@+id/desc_title"
                tools:layout_editor_absoluteX="68dp" />

            <ImageView
                android:id="@+id/desc_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:src="@drawable/image_ex"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/desc_description" />

            <TextView
                android:id="@+id/desc_context"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:fontFamily="@font/roboto"
                android:text="context"
                android:textColor="#000000"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/desc_image" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/saved_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="32dp"
        android:clickable="true"
        android:src="@drawable/float_p"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
解决思路与方案
  • 排查数据源是否异常:在Fragment中打印从NewsApi获取的原始文本数据,确认接口返回的内容本身是否存在重复、乱码或未格式化的情况。如果数据源本身有问题,需要对接口返回的文本进行清洗(比如去除重复片段、处理转义字符)。
  • 优化TextView的文本显示设置:给显示大文本的desc_description和desc_context添加以下属性,优化长文本排版与换行:
    android:breakStrategy="balanced"
    android:hyphenationFrequency="normal"
    android:maxLines="1000" <!-- 按需设置最大行数,避免无限制占用内存 -->
    
  • 清理布局中的无效属性:移除desc_description上的tools:layout_editor_absoluteX="68dp",该属性仅用于布局编辑器预览,运行时可能导致布局偏移,影响文本显示。
  • 检查数据绑定逻辑:确认Fragment中给TextView赋值的代码是否存在重复调用setText()的情况,比如在onResume()或其他生命周期方法中重复设置文本,导致内容被多次追加。
  • 优化ScrollView的渲染性能:如果文本极大,可以考虑使用androidx.core.widget.NestedScrollView替代ScrollView,或者给ScrollView添加android:fastScrollEnabled="true"提升滚动体验,同时避免因内存占用过高导致的渲染异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:02:51