【发布时间】:2012-11-01 06:17:41
【问题描述】:
我使用下面的代码来计算字符串的宽度,但是宽度的结果与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;
【问题讨论】:
-
Duplicated。你们应该在询问之前标记重复以鼓励新人搜索。
-
非常感谢您提供的链接。下次我会非常仔细地搜索。
标签: android