渲染前如何获取textview的行数? [英] How to get line count of textview before rendering?
本文介绍了渲染前如何获取textview的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在呈现字符串之前获取字符串在 TextView
中占用的行数.
How can I get the number of lines a string will take up in a TextView
before it is rendered.
A ViewTreeObserver
将不起作用,因为它们仅在渲染后才会被触发.
A ViewTreeObserver
will not work because those are only fired after it is rendered.
推荐答案
final Rect bounds = new Rect();
final Paint paint = new Paint();
paint.setTextSize(currentTextSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);
现在将文本的宽度除以 TextView 的宽度以获得总行数.
Now divide the width of text with the width of your TextView to get the total number of lines.
final int numLines = (int) Math.ceil((float) bounds.width() / currentSize);
currentSize :将呈现文本的视图的预期大小.尺寸不应超过屏幕宽度.
currentSize : Expected size of the view in which the text will be rendered. The size should not go beyond the screen width.
这篇关于渲染前如何获取textview的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文