设置mTv.setEllipsize(TruncateAt.END)后,当显示的内容过长时会在结尾用“...”代替;
但是产品和UI都觉得三个显的太长,其中字母显示宽度不同,大小写的显示宽度也不同。
我的修改如下:
private void handleTextView (Context context, String content) {
if (content == null)
return;
char[] textCharArray = content.toCharArray();
//已绘的宽度
float drawedWidth = 0;
float charWidth;
//add by csj 13-12-25
if (Launcher.displayMetrics.widthPixels >= 480 && Launcher.displayMetrics.widthPixels <720) {
textShowWidth = context.getResources().getDimension(R.dimen.other_text_show_width);
} else {
textShowWidth = context.getResources().getDimension(R.dimen.text_show_width);
}
float pointWidth = paint.measureText("..");
for (int i = 0; i < textCharArray.length; i++) {
charWidth = paint.measureText(textCharArray, i, 1);
if (textShowWidth - drawedWidth - pointWidth < charWidth) {
isShow = true;
mTv.setText(content.subSequence(0, i)+"..");
return;
} else {
drawedWidth += charWidth;
}
}
if (!isShow) {
mTv.setText(content);
}
isShow = false;
}