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

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

问题描述

我有一个 EditText、一个 Button 和一个 TextView.单击按钮时,textview 会显示用 edittext 编写的文本.是否可以根据文本找到占用的文本视图的大小.即如果它有三个字符abc",那么现在宽度是多少,如果它有五个字符,比如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天全站免登陆