Android和计算一个给定的字体和字体大小单行string的大小?

是否有一个API方法来计算一行中显示的string大小(即宽度和高度)以及给定的字体和字体大小?

Solutions Collecting From Web of "Android和计算一个给定的字体和字体大小单行string的大小?"

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. 
 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(); 

Paint类有一个方法measureText(),它会给你浮动的宽度

 Paint p = new Paint(); int INFO_WINDOW_WIDTH = (int)p.measureText("this is string"); 

另一个是Paint.getTextBounds