Android应用不能很好地呈现HTML标签

时间:2017-02-22 11:22:43

标签: java android html android-studio

这是我从字符串中获取Web服务的内容。

<p><strong>This is instructor's reply to the guest's MDB message</strong></p>

这就是我将其设置为文本视图的方式

String Reply = parent.getString(TAG_ReplyMessage);
TextView ReplyTextView= (TextView) findViewById(R.id.reply_txt);
ReplyTextView.setText(Html.fromHtml(Reply));

但应用会显示包含<p><strong>代码的信息。

enter image description here

它应该呈现那些标签,而不是显示它的HTML。

任何帮助都将受到高度赞赏。

4 个答案:

答案 0 :(得分:3)

它可能对您有所帮助 我的项目也遇到了同样的问题,这对我有用。可能会对你有所帮助:

String Reply = parent.getString(TAG_ReplyMessage);
TextView ReplyTextView= (TextView) findViewById(R.id.reply_txt);
String htmltext=Html.fromHtml(Reply).toString();
ReplyTextView.setText(Html.fromHtml(htmltext));

答案 1 :(得分:0)

您可以直接在<![CDATA[...]]>中的strings.xml之间编写这些标记。

示例:

<string name="type"><![CDATA[Type:<B> %s</B>]]></string>

在Java中:

textView.setText(Html.fromHtml(getString(R.string.type, "Sports")));

输出:

输入:体育

更新:

如果字符串本身如下:

String data = "&lt;p&gt;&lt;strong&gt;This is instructor&amp;#39;s reply to the guest&amp;#39;s MDB message&lt;/strong&gt;&lt;/p&gt;";

然后,您可以尝试如下:

textView.setText(Html.fromHtml(Html.fromHtml(data).toString()));

答案 2 :(得分:0)

Android不支持所有HTML标记。

这是一个受支持的HTML标记列表,虽然有点旧: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/

或者您可以查看来源并查看支持的内容:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/text/Html.java#Html

根据这些列表,不支持<p>。所以它无法解析<p></p>中的任何内容。

答案 3 :(得分:0)

&amp;#39;删除amp;部分。 '单引号的Html代码只是&#39;