android studio|android Html.fromHtml使用详解

时间:2020-10-26  来源:文本特效  阅读:

项目中往往需要显示一段文本,如果对文本需要特定的效果,就要写自定义的span,这样的工作量会变得很大,目前android支持html格式的文本。提供了两个接口,下面我们就来看一下怎么使用。

1. Spanned android.text.Html.fromHtml(String source) //输入的参数为(html格式的文本)

目前android不支持全部的html的标签,目前只支持与文本显示和段落等标签,对于图片和其他的多媒体,还有一些自定义标签不能识别;

例子:


TextView t3 = (TextView) findViewById(R.id.text3);  
t3.setText(Html.fromHtml( "text3:  Text with a " + "linkhttp://www.google.com\">link " +"created in the Java source code using HTML.")); 

另外也可以在string.xml中使用,但是要用用去转义。如下例子:



   Text with markup for [b]bold[/b]
 and [i]italic[/i] text.
 
 There is also support for a
 teletype-style font.
 But no use for the code
 tag!
 
 ]]>

 
TextView view = (TextView)findViewById(R.id.sampleText);
String formattedText = getString(R.string.htmlFormattedText);
Spanned result = Html.fromHtml(formattedText);
view.setText(result);
 

2.Spanned android.text.Html.fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler)

Source:    需处理的html文本
imageGetter :对图片处理(处理html中的图片标签)

tagHandler  :对标签进行处理(相当于自定义的标签处理,在这里面可以处理自定义的标签)

具体不细说,大家感兴趣可以自行学习;在实际项目里我们就可以使用第一种方式来替代之前我们用的自定义span了。

android studio|android Html.fromHtml使用详解

http://m.bbyears.com/wangyetexiao/106993.html

推荐访问:android学习路线
相关阅读 猜你喜欢
本类排行 本类最新