如果可能的话,我也想要身高。
发布于 2010-09-03 03:05:40
可以使用Paint对象的getTextBounds(String text, int start, int end, Rect bounds)
方法。您可以使用TextView
提供的paint对象,也可以自己构建具有所需文本外观的paint对象。
使用Textview,您可以执行以下操作:
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.height();
int width = bounds.width();
发布于 2012-06-17 08:55:57
如果你只需要宽度,你可以使用:
float width = paint.measureText(string);
http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String)
发布于 2016-12-09 19:07:15
我用这种方法测量过宽度:
String str ="Hiren Patel";
Paint paint = new Paint();
paint.setTextSize(20);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
Rect result = new Rect();
paint.getTextBounds(str, 0, str.length(), result);
Log.i("Text dimensions", "Width: "+result.width());
这会对你有帮助。
https://stackoverflow.com/questions/3630086
复制相似问题