【发布时间】:2016-07-06 04:32:06
【问题描述】:
如果 html 内容超过 5 行,我也想在 textview 中显示它,然后修剪额外的行并在末尾添加“...”。我使用这些代码来添加修剪额外的行,
textview.setText(Html.fromHtml(message));
this.setAutoLinkMask(Linkify.ALL);
this.setMaxLines(5);
this.setEllipsize(TextUtils.TruncateAt.END);
this.setHorizontalScrollBarEnabled(true);
但它仅适用于某些文本,
ViewTreeObserver vto = feedListRowHolder.description.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = feedListRowHolder.description.getViewTreeObserver();
if (Build.VERSION.SDK_INT < 16) obs.removeGlobalOnLayoutListener(this);
else obs.removeOnGlobalLayoutListener(this);
if (feedListRowHolder.description.getLineCount() > 5) {
int lineEndIndex = feedListRowHolder.description.getLayout().getLineEnd(4);
String text = textview.subSequence(0, lineEndIndex - 3) + "...";
textview.setText(Html.fromHtml(text));
}
}
});
问题是它从文本中删除了 html 格式。
【问题讨论】:
标签: android html textview ellipse