<分区>
Possible Duplicate:
How to get string width on Android?
我使用下面的代码来计算字符串的宽度,但是结果的宽度与TextView中显示的宽度有很大的不同。我怎样才能正确地做到这一点?感谢您的建议。
Paint paint= new Paint();
paint.setTextSize(size);
int iRet = 0;
if (str != null && str.length() > 0) {
int len = str.length();
float[] widths = new float[len];
paint.getTextWidths(str, widths);
for (int j = 0; j < len; j++) {
iRet += (int) Math.ceil(widths[j]);
}
}
return iRet;