在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本
框中的字符串进行HTML格式化,支持的还是很多的,
但要注意的是要在string.xml中用<!--cdata-->去转义,比如:
Java代码
<string name="htmlFormattedText">
<![CDATA[
Text with markup for [b]bold[/b]
and [i]italic[/i] text.
There is also support for a
<tt>teletype-style</tt> font.
But no use for the <code>code</code>
tag!
]]></string>
上面就用到了大量的HTML标签了,JAVA代码中这样使用:
Java代码
TextView view = (TextView)findViewById(R.id.sampleText);
String formattedText = getString(R.string.htmlFormattedText);
Spanned result = Html.fromHtml(formattedText);
view.setText(result);
或者是这样写:
view.setText(Html.fromHtml("<u>测试html标签</u>"));
本文介绍了如何在Android中使用Html.fromHtml方法将字符串转换为带有HTML标记的格式化文本。通过在strings.xml文件中使用CDATA段落可以正确地解析HTML标签。
1885

被折叠的 条评论
为什么被折叠?



