TextView系列:TextView设置滚动

2022-07-31 12:56:53

上效果。

  • 通常,在做隐私协议的时候,后端会给一个包含html标签的字符串str,客户端通过Html.fromHtml(str)获取一个Spanned对象span,最后textView.setText(span)Spanned是接口CharSequence的实现类。这种做法,TextView需要设置滚动。
  tv.setMovementMethod(new ScrollingMovementMethod());
  • 有需求,也可以再加个滚动条,android:scrollbars="vertical"
  • 完整布局文件
<?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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:background="#ff0000"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:maxHeight="40dp"
        android:text="强盗的本质是破格获取,破格获取和直接获取是两个不同的概念。你们没有自信与强者在同一个规则下竞争,这只能说明你是弱者,因为弱势文化所追求的最高价值就是破格获取。所以,强盗的逻辑从本质上讲是懦弱的生存哲学,所以你不算好汉。"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 作者:zhangjin1120
  • 原文链接:https://blog.csdn.net/zhangjin1120/article/details/116738942
    更新时间:2022-07-31 12:56:53