android - 如何使 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自动。
解决方案
将您的文本视图放在 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);
}
});
推荐阅读
- javascript - Javascript更改HTML给出错误
- javascript - 无法在 JavaScript 中导入 AlertifyJS
- indexing - Julia - 以元组数组作为索引的 JuMP 约束
- ios - 通过蓝牙将媒体密钥发送到 iOS 设备
- python - 做一个 groupby 和一个 sum 但在 Pandas DataFrame 中保持其他列完整
- javascript - 如何按比例缩放组件/页面,如 Ctrl +/-
- qt - 如何使 QAbstractSpinBox 自动重复向上/向下按钮?
- javascript - 无法将变量设置为 Bcrypt 生成的哈希
- python - 如何避免使用 Autoreg 或 ARIMA Statsmodels 进行动态预测
- c# - 未捕获任务错误