如何获得字符串的宽度在Android? [英] How to get string width on Android?

查看:85
本文介绍了如何获得字符串的宽度在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得高太多,如果可能的。

I would like to get height too if possible.

推荐答案

您可以使用getTextBounds(字符串文本,诠释开始,诠释年底,矩形边界)一个油漆对象的方法。您可以通过使用一个TextView提供的油漆对象或构建您只需用自己想要的文本外观。

You can use the getTextBounds(String text, int start, int end, Rect bounds) method of a Paint object. You can either use the paint object supplied by a TextView or build one yourself with your desired text appearance.

使用一个TextView你能做到以下几点:

Using a Textview you Can do the following:

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

这篇关于如何获得字符串的宽度在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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