Android(安卓)Html.fromhtml
16lz
2021-01-26
在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本
框中的字符串进行HTML格式化,支持的还是很多的,
但要注意的是要在string.xml中用<!--cdata-->去转义,比如:
Java代码
<string name="htmlFormattedText">
<![CDATA[
Text with markupfor[b]bold[/b]
and [i]italic[/i] text.
There is also supportfora
<tt>teletype-style</tt> font.
But no useforthe <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>今天礼拜天</u>"));
更多相关文章
- Android笔记汇总
- 解决Android文档打开慢的问题
- 利用SonarQube检测Android(安卓)studio 代码
- 用React Native做一个填字游戏(3)——背景音乐
- AndroidAnnotations的使用
- Android(安卓)ViewFlipper 用例
- Andorid TabHost 使用小结
- Android(安卓)GridView控件 使用
- Android资料(书籍+代码)