How can I make a TextView automatically scroll as

2019-02-01 18:10发布

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" />

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.

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?

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

8条回答
家丑人穷心不美
2楼-- · 2019-02-01 19:06

Had the same question. Tried several decisions from this and similar discussions, nothing worked. Solved it this way:

edtConsoleText.setSelection(edtConsoleText.getText().length());

after every .append() .

查看更多
forever°为你锁心
3楼-- · 2019-02-01 19:08

As per answer here Making TextView Scrollable in Android

You don't need to use a ScrollView actually.

Just set the

android:maxLines = "AN_INTEGER"

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

Then use:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

That will work..

查看更多
登录 后发表回答