首页 > 解决方案 > 如何使 Android textview 可选择并滚动到底部(默认情况下)?

问题描述

我有一个文本视图,默认情况下必须选择textView.setTextIsSelectable(true)和滚动到底部的文本textView.setMovementMethod(ScrollingMovementMethod.getInstance())

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="@color/colorScreenBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xxxx.xxxx.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical"
    android:weightSum="100">

    <TextView
        android:id="@+id/result"
        android:scrollbars = "vertical"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:elevation="8dp"
        android:textIsSelectable="true"
        android:gravity="bottom"
        android:background="@color/white"
        android:padding="5dp"
        android:focusable="true"
        android:fastScrollEnabled="true"
        android:fastScrollAlwaysVisible="true"
        android:scrollbarSize="7dip"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60"
        android:background="@color/gray_background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
            tools:listitem="@layout/recyclerview_item"
            android:background="@color/gray_background"
            />
    </androidx.core.widget.NestedScrollView>
</LinearLayout>

问题是setMovementMethod禁用 textview 可选择性。

如何解决这个问题?

注意:这里需要的主要功能是不仅 textview 必须是可滚动的,而且默认情况下它还必须最初滚动到底部setMovementMethod(ScrollingMovementMethod.getInstance()),这是由提供的,根据How to get the Android TextView to scroll down to end自动

标签: androidscrolltextview

解决方案


将您的文本视图放在 xml 的滚动视图中

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">
    <TextView
        android:id="@+id/textView"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        android:textIsSelectable="true"
        android:gravity="bottom"
        android:background="@color/white"
        android:padding="5dp"
        android:text="@string/dummy"
        android:focusable="true"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
    </ScrollView>

然后在你的课堂上做这个

//This will scroll to bottom 
ScrollView scrollView=findViewById(R.id.scrollView);
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

推荐阅读