滚动一个TextView到指定的行 [英] Scrolling a TextView to a specific line
问题描述
我有内部的滚动型一个TextView。比方说,在滚动型被命名为取值
和TextView中被命名为 T
。
I have a TextView inside a ScrollView. Let's say the ScrollView is named s
and the TextView is named t
.
我有很多线显示在TextView中,并在同一时间,我想滚动视图到指定的行。
I have many lines to be displayed in the TextView and at the same time I want to scroll the view to a specific line.
所以,我没有这样的:
t.setText(aVeryLongString);
int y = t.getLayout().getLineTop(40); // e.g. I want to scroll to line 40
s.scrollTo(0, y);
但它不会滚动,除了第二次。似乎在第一时间的code完成时,滚动型知道多少TextView的的总高度。
But it won't scroll, except the second time. It seems that on the first time the code finishes, the ScrollView knows how much the total height of the TextView is.
所以我觉得一定有什么给力计算所需的高度scrollTo调用之前。如何做到这一点(或其他方式)?
So I think there must be something to force calculating the needed height before the scrollTo call. How to do that (or otherwise)?
推荐答案
找到了答案<一href="http://groups.google.com/group/android-developers/browse%5Fthread/thread/59b6cc97206d2f8d">here.
而不是调用的 scrollTo
直接,我们必须调用后
而不是在滚动型。这工作。
Instead of calling scrollTo
directly, we must call post
instead on the ScrollView. This works.
t.setText(aVeryLongString);
s.post(new Runnable() {
@Override
public void run() {
int y = t.getLayout().getLineTop(40); // e.g. I want to scroll to line 40
s.scrollTo(0, y);
}
});
这篇关于滚动一个TextView到指定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!