android中獲取文字的寬度

在開發中,有時候要獲取文字的寬度以更好的佈局,偶然的機會看了《professional android app devlopment》發現了這樣的寫法,實在太酷了。


Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
// Define the string.
String displayText = “Hello World!”;
// Measure the width of the text string.
float textWidth = mTextPaint.measureText(displayText);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章