Android textview ellipse删除了html内容

时间:2016-07-06 04:32:06

标签: android html textview ellipse

我想在textview中显示html内容,如果超过5行,则修剪其他行并添加" ..."在末尾。我使用这些代码来添加修剪附加行

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格式。

1 个答案:

答案 0 :(得分:0)

更改此String text = textview.subSequence(0, lineEndIndex - 3) + "...";

String text = message.subSequence(0, lineEndIndex - 3) + "...";