添加更多文本行时,如何使 TextView 自动滚动? [英] How can I make a TextView automatically scroll as I add more lines of text?

查看:19
本文介绍了添加更多文本行时,如何使 TextView 自动滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个这样的 TextView:

So, I have a TextView like so:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="140.7dp"
    android:id="@+id/terminalOutput"
    android:layout_marginBottom="0.0dp"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:maxLines="8" />

我将它用作一种运行日志,显示给用户,以便他们可以监控大约需要 3 分钟的任务的进度.但是,一旦我超过 8 行,文本就会离开屏幕.这对用户来说是不直观的,因为除了通过尝试向下滚动来手动轮询之外,他们无法知道它离开了屏幕.

I use it as a sort of running log, displayed to the user so they can monitor progress of a task that takes about 3 minutes. However, once I go over 8 lines, the text goes off screen. This is unintuitive to the user because they have no way of knowing that it went off screen, other than to manually poll by trying scroll down.

我怎样才能做到这样每次我向这个 TextView 添加一些文本时,我都会让它向下滚动到尽可能低的位置?

How can I make it so that every time I add some text to this TextView I make it scroll down as low as it can go?

此外,这是在 Xamarin Android 中,但我认为这无关紧要.它和 Java 之间的转换很容易

Also, this is in Xamarin Android, but I don't think it's relevant. It's easy to translate between it and Java

推荐答案

按照这里的回答 在 Android 中让 TextView 可滚动

您实际上不需要使用 ScrollView.

You don't need to use a ScrollView actually.

只需设置

android:maxLines = "AN_INTEGER"

android:scrollbars = "垂直"布局的 xml 文件中 TextView 的属性.

android:scrollbars = "vertical" properties of your TextView in your layout's xml file.

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在您的代码中.

这会起作用的..

这篇关于添加更多文本行时,如何使 TextView 自动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆