查詢了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());