`
jackeysion
  • 浏览: 127623 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Android下如何计算要显示的字符串所占的宽度和高度

阅读更多
查询了google发现在android一下有几种方法可以做到,但是经过实际测试发现下面这种方法是最准确的
Rect bounds = new Rect();
String text = "Hello World";
TextPaint paint;

paint = findViewById(R.id.hello_world).getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();


Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("豆", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics