简体   繁体   English

“如何在textview上显示android中的div html内容

[英]"How to display div html content in android on textview

I have value on string: 我对字符串有价值:

String htmlValue = "<div style="text-align: center;"><span style="font-family: impact, chicago; font-size: 18px;">Exchange Offer !!&nbsp;</span></div>
    <div style="text-align: center;"><span style="font-size: 14px;">any mobile phone</span> , <span style="font-size: 14px;">give your</span> <span style="font-family: impact, chicago; font-size: 20px;">take new one</span>.</div>";

and how to display such type div included in textview. 以及如何显示textview中包含的此类型div。

I have try this but only content display div styling could not display. 我试过这个,但只有内容显示div样式无法显示。

Spanned result= Html.fromHtml(htmlValue);
tvFooter.setText(result);

or 要么

 tvFooter.setText(Html.fromHtml(htmlValue));

Here Defined: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html , div supported but could get exact expected output by doing above. 这里定义: http//commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html ,支持div,但通过上面的操作可以获得准确的预期输出。 I have both of above trick, but not working. 我有两个上面的技巧,但没有工作。

Error Output: 错误输出:

在此输入图像描述

Expected Output like this 像这样的预期输出

: 在此输入图像描述

As I scanned the source code of /frameworks/base/core/java/android/text/Html.java , the attribute style and the tag span are not supported. 在我扫描/frameworks/base/core/java/android/text/Html.java的源代码时,不支持属性style和标记span Suggest using webview instead of textview 建议使用webview而不是textview

Following tags or attributes seems to be supported in Lollipop, Lollipop似乎支持以下标签或属性,

 - <br>
 - <p>
 - <div>
 - <strong>
 - <b>
 - <em>
 - <cite>
 - <dfn>
 - <i>
 - <big>
 - <small>
 - <font color="..." face="...">
 - <blockquote>
 - <tt>
 - <monospace>
 - <a href="...">
 - <u>
 - <sup>
 - <sub>
 - <h1>
 - <h2>
 - <h3>
 - <h4>
 - <h5>
 - <h6>
 - <img src="...">

You need to write \\ before inside " because they're closing your string before they properly end, like Jemshit Iskenderov says. 你需要在内部写“\\”因为他们在正确结束之前关闭你的字符串,就像Jemshit Iskenderov所说。

Also, the tags style and span can't be shown in a textview. 此外,标签样式和范围无法在文本视图中显示。 Check this link for a list of HTML tags that work in textview. 检查此链接以获取在textview中工作的HTML标记列表。

All html tags are not supported by Html.fromHtml() check here the supported tags in textview.So better you use webview instead of textview to load the html content. Html.fromHtml()不支持所有html标签。 请在此处查看 textview中支持的标签。更好地使用webview而不是textview来加载html内容。 like 喜欢

 yourwebview.loadData(yourhtmlData, "text/html", "UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM