查询了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());
本文介绍了一种在Android中准确测量字符串宽度的方法。通过使用`Rect`和`TextPaint`类,该方法能精确获取文本的实际绘制尺寸,适用于各种字体大小和样式。
2339

被折叠的 条评论
为什么被折叠?



