如何获得TextView中的行数渲染过吗? [英] How to get line count of textview before rendering?

查看:104
本文介绍了如何获得TextView中的行数渲染过吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得的行数的字符串将在一个的TextView 被渲染之前。

A ViewTreeObserver 将无法工作,因为这些被渲染后,仅启动。

解决方案

 矩形边界=新的矩形();
涂料粉刷=新的油漆();
paint.setTextSize(currentSize);
paint.getTextBounds(的TestString,0,testString.length(),边界);

INT宽度=(int)的Math.ceil(bounds.width());
 

现在devide文本的宽度与你的TextView的宽度,并得到总线路。

How can I get the number of lines a string will take up in a TextView before it is rendered.

A ViewTreeObserver will not work because those are only fired after it is rendered.

解决方案

Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextSize(currentSize);
paint.getTextBounds(testString, 0, testString.length(), bounds);

int width = (int) Math.ceil( bounds.width());

Now devide the width of text with the width of your TextView and get the total lines.

这篇关于如何获得TextView中的行数渲染过吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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