如何在Android中以编程方式找到TextView的文本区域(高度/宽度) [英] How to find the Text Area(Height/Width) of TextView programmatically in android

查看:687
本文介绍了如何在Android中以编程方式找到TextView的文本区域(高度/宽度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText,一个Button和一个TextView.单击按钮后,textview将显示以edittext编写的文本.是否有可能找到根据文本占用的textview的大小.即,如果它有三个字符" abc ",那么现在的宽度是多少,如果它有5个字符,如" abcde ",那么宽度是多少?

I have an EditText, a Button and a TextView. On clicking the button, textview shows the text written in edittext. Is it possible to find the size of textview occupied depending upon text. i.e. If It has three characters "abc", what is width now, if it has 5 characters like "abcde" , then what is the width ?

推荐答案

Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();

textView.setText("bla");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();

这篇关于如何在Android中以编程方式找到TextView的文本区域(高度/宽度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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