是否有一种API方法可以计算一行显示的字符串的大小(即宽度和高度)以及给定的字体和字体大小?
答案 0 :(得分:9)
Paint p = new Paint();
p.setTypeface(TypeFace obj); // if custom font use `TypeFace.createFromFile`
p.setTextSize(float size);
float textWidth = p.measureText("Your string");
// you get the width here and textsize is the height.
答案 1 :(得分:8)
Paint mPaint = new Paint();
mPaint.setTextSize(/*put here ur size*/);
mPaint.setTypeface(/* put here ur font type */);
Rect bounds = new Rect();
mPaint.getTextBounds(text, 0, text.length(), bounds);
然后拨打
bounds.width();
bounds.height();
答案 2 :(得分:2)
Paint类有一个方法measureText(),它将为您提供float中的宽度
Paint p = new Paint();
int INFO_WINDOW_WIDTH = (int)p.measureText("this is string");