Android html.fromhtml Use detailed

Source: Internet
Author: User
Tags cdata html tags

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.