我不知道您的問題的確切答案,但通常您可以使用圖形庫中可用的方法計算基於字體類型和大小的文本的寬度和高度。
我已經在C#和Java中完成了它。在C#中稱爲「MeasureString」,在Java中稱爲「FontMetrics」。
編輯:
看看這個代碼是有用的(因爲我沒有在這裏Android SDK中我還沒有編譯它):
String myText="";
String tempStr="";
int startIndex=0;
int endIndex=0;
//calculate end index that fits
endIndex=myPaint.breakText(myTest, true, frameWidth, null)-1;
//substring that fits into the frame
tempStr=myText.substring(startIndex,endIndex);
while(endIndex < myText.length()-1)
{
//draw or add tempStr to the Frame
//at this point
//set new start index
startIndex=endIndex+1;
//substring the remaining of text
tempStr=myText.substring(startIndex,myText.length()-1);
//calculate end of index that fits
endIndex=myPaint.breakText(tempStr, true, frameWidth, null)-1;
//substring that fits into the frame
tempStr=myText.substring(startIndex,endIndex);
}
類似的方法也可用於Android:
breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth)
測量文本,如果測量的寬度超過maxWidth,則提前停止。
measureText(String text)
返回文本的寬度。
http://developer.android.com/reference/android/graphics/Paint.html
難道u檢驗的代碼? – 2010-12-12 09:10:19