如何获取TextView的行数? [英] How to get number of lines of TextView?
本文介绍了如何获取TextView的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取一个文本视图的行数
I want to get the number of lines of a text view
textView.setText("Test line 1 Test line 2 Test line 3 Test line 4 Test line 5.............")
textView.getLineCount();
总是返回 zero
那我也试过了:
ViewTreeObserver vto = this.textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = textView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
System.out.println(": " + textView.getLineCount());
}
});
它返回准确的输出.
但这仅适用于静态布局.
But this works only for a static layout.
当我动态扩展布局时,这不再起作用了.
When I am inflating the layout dynamically this doesn't work anymore.
如何找到 TextView 中的行数?
How could I find the number of line in a TextView?
推荐答案
我能够使用 post
让 getLineCount()
不返回 0,如下所示:
I was able to get getLineCount()
to not return 0 using a post
, like this:
textview.setText("Some text");
textview.post(new Runnable() {
@Override
public void run() {
int lineCount = textview.getLineCount();
// Use lineCount here
}
});
这篇关于如何获取TextView的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文