我有一个里面有 html 的文本视图。在设备上它可以正确呈现,但在预览中它看起来像纯 HTML。
是否可以在图形布局工具而不是纯 HTML 中看到最终结果?
提前致谢!
我有一个里面有 html 的文本视图。在设备上它可以正确呈现,但在预览中它看起来像纯 HTML。
是否可以在图形布局工具而不是纯 HTML 中看到最终结果?
提前致谢!
所以我知道很多时间过去了,但我找到了一种在 Android Studio 的布局编辑器中预览 HTML 的方法(虽然不知道 Eclipse)。
所以我刚刚创建了一个自定义的 TextView callet HtmlTextView:
public class HtmlTextView extends TextView {
    public HtmlTextView(Context context) {
        super(context);
    }
    public HtmlTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setText(CharSequence text, BufferType type) {
        Spanned html = null;
        if(text != null) {
            html = Html.fromHtml(text.toString());
            super.setText(html, BufferType.SPANNABLE);
        } else {
           super.setText(text, type);
        }
    }
}
之后,只需使用它:
<com.example.app.custom.views.HtmlTextView
    android:text="@string/your_html_string"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" />
字符串资源如下所示:
<string name="your_html_string"><![CDATA[My <b><font color=\'#E55720\'>AWESOME</font></b> html]]></string>
希望它可以帮助某人!
AFAIK Android 不会在图形布局工具的 TextView 中显示 HTML 的呈现。它只会显示您何时运行您的应用程序。
该类TextView已经支持一些基本的 html 标签,使用Html.fromHtml.
不够清楚,但试试这个
tv.setText(Html.fromHtml(yourString), TextView.BufferType.SPANNABLE);
或尝试使用 webview 而不是 TextView
webview.loadData(yourString,"text/html","utf-8");
我的 Eclipse Layout-Preview 没有显示罢工标签,所以我扩展了@Arthur 的答案:
public class HtmlTextView extends TextView {
CustomHtmlTagHandler tagHandler = new CustomHtmlTagHandler();
public HtmlTextView(Context context) {
    super(context);
}
public HtmlTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
public void setText(CharSequence text, BufferType type) {
    Spanned html = null;
    if(text != null) {
        html = Html.fromHtml(text.toString(), null, tagHandler);
        super.setText(html, BufferType.SPANNABLE);
    } else {
       super.setText(text, type);
    }
}
}
这是 CustomHtmlTagHandler:
public class CustomHtmlTagHandler implements TagHandler {
public void handleTag(boolean opening, String tag, Editable output,
        XMLReader xmlReader) {
    if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
        processStrike(opening, output);
    }
}
private void processStrike(boolean opening, Editable output) {
    int len = output.length();
    if(opening) {
        output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
    } else {
        Object obj = getLast(output, StrikethroughSpan.class);
        int where = output.getSpanStart(obj);
        output.removeSpan(obj);
        if (where != len) {
            output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
private Object getLast(Editable text, Class kind) {
    Object[] objs = text.getSpans(0, text.length(), kind);
    if (objs.length == 0) {
        return null;
    } else {
        for(int i = objs.length;i>0;i--) {
            if(text.getSpanFlags(objs[i-1]) == Spannable.SPAN_MARK_MARK) {
                return objs[i-1];
            }
        }
        return null;
    }
}
}