如何将所有Html样式应用于textview

时间:2015-09-30 05:57:07

标签: android html

我试图在原生页面中显示HTML内容。我已成功parsing HTML个内容并在Textview中显示。但是,大多数HTML样式不适用于Textview。例如:

<p style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\">

此处colorfont-amilyfont-size和其他一些属性无效。

我使用Html.fromHtml(html)方法设置html内容。 有没有办法扩展HTML类并将这些样式添加到它?

还有其他选择。

3 个答案:

答案 0 :(得分:0)

尝试以下,

TextView mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" + 
            "<small>" + description + "</small>" + "<br />" + 
            "<small>" + DateAdded + "</small>"));

答案 1 :(得分:0)

以下是示例:

myView.setHint(Html.fromHtml("<small><small><small>" + 
     getString(R.string.hint) + "</small></small></small>"));

此处提供更多详细信息:http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

答案 2 :(得分:0)

@Amanda Fernandez: Android API文档没有规定支持哪些HTML标记。目前android似乎支持在TextView上呈现以下HTML标记。

    <a href=”…”> <b>,  <big>, <blockquote>, <br>, <cite>, <dfn>
<div align=”…”>,  <em>, <font size=”…” color=”…” face=”…”>
<h1>,  <h2>, <h3>, <h4>,  <h5>, <h6>
<i>,  <img src=”…”>,  <p>, <small>
<strike>,  <strong>, <sub>, <sup>, <tt>, <u>

请检查How To Display HTML In Android TextView

  

How to display HTML in TextView?