歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Andorid中Html.fromHtml方法

Andorid中Html.fromHtml方法

日期:2017/3/1 10:10:53   编辑:Linux編程

在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>今天禮拜五</u>"));

Copyright © Linux教程網 All Rights Reserved