The project often needs to display a piece of text, if the text requires a specific effect, write a custom span, the workload will become very large, the current Android support HTML-formatted text. With two interfaces, let's take a look at how to use them.
1. Spanned android.text.Html.fromHtml (String source)//input parameter is (text in Html format)
Currently Android does not support all HTML tags, currently only supports with text display and paragraph labels, for pictures and other multimedia, and some custom tags are not recognized;
Example:
TextView t3 = (TextView) Findviewbyid (R.ID.TEXT3);
T3.settext (html.fromhtml ("<b>text3:</b> Text with a" + "<a href=\" http://www.google.com\ ">link< /a> "+" created in the Java source code using HTML. ");
You can also use it in String.xml, but use <!–cdata–> to escape. The following example:
<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>
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 text to be processed
Imagegetter: Processing of pictures (processing of picture tags in html)
Taghandler: Handles labels (equivalent to custom label processing, where custom tags can be processed)
Specifically do not elaborate, we are interested can learn by themselves; In the actual project we can use the first way to replace the custom span we used before.