Loading HTML display using TextView in Android

Source: Internet
Author: User

Today I learned how to load HTML in TextView to display text, images, and other information;

First, let's take a look at a Textview attribute android: autoLink to parse some special characters in Textview, such as: web, email, phone, map ,.. you can also use android: textColorLink to set the color of these special characters, and other attributes to set the font size, color, and so on. Although these can all implement the required functions, however, Google also provides an Html class to directly load HTML code. Learn together.

(1): Let's look at the inheritance diagram of the Html class: This directly inherits the Object class:

 

This class can display Html code converted to the corresponding text, but not all HTML tags are supported.

Html mainly includes the following methods:

1: public static Spanned fromHtml (String source)

This method returns the displayed text. However, if an img tag is displayed, the android system will directly use a default image, waiting for us to load the image, replace the loaded images,


[Java] <span style = "font-size: 16px;"> _ TextView
. SetText (Html
. FromHtml ("<B> Jiangsu </B> "


The effect is as follows:

How can I display an image that cannot be displayed in the lower-right corner? Next, let's find the overloaded fromHtml method...

2: public static Spanned fromHtml (String source, Html. ImageGetter imageGetter, Html. TagHandler tagHandler)

①: In this overload method, the second parameter is an ImageGetter interface. Here we implement a public abstract Drawable getDrawable (String source) method for this interface. This method is used to load images. [Note: you must call the setbounds () method to set the image size]


[Java] <span style = "font-size: 18px;"> </span> <span style = "font-size: 16px;"> ImageGetter _ ImageGetter = new Html. imageGetter (){
@ Override
Public Drawable getDrawable (String source ){
If (source! = Null ){
BitmapDrawable _ BitmapDrawable = (BitmapDrawable) getResources ()
. GetDrawable (R. drawable. tu );
_ BitmapDrawable. setBounds (0, 0,100,100 );
Return _ BitmapDrawable;
}
Return null;
}
}; </Span>
<Span style = "font-size: 18px;"> </span> <span style = "font-size: 16px;"> ImageGetter _ ImageGetter = new Html. imageGetter (){
@ Override
Public Drawable getDrawable (String source ){
If (source! = Null ){
BitmapDrawable _ BitmapDrawable = (BitmapDrawable) getResources ()
. GetDrawable (R. drawable. tu );
_ BitmapDrawable. setBounds (0, 0,100,100 );
Return _ BitmapDrawable;
}
Return null;
}
}; </Span> the image in the resource library is returned for convenience:



② The third parameter is a TagHandler interface, which implements the public abstract void handleTag (boolean opening, String tag, Editable output, XMLReader xmlReader) method in this interface to parse tags, especially those tags that are not in HTML can be understood as custom tags...


[Java] TagHandler _ TagHandler = new Html. TagHandler (){
@ Override
Public void handleTag (boolean opening, String tag, Editable output,
XMLReader xmlReader ){
Toast. makeText (TestActivity. this, tag, 2000). show ();
}
};
TagHandler _ TagHandler = new Html. TagHandler (){
@ Override
Public void handleTag (boolean opening, String tag, Editable output,
XMLReader xmlReader ){
Toast. makeText (TestActivity. this, tag, 2000). show ();
}
};

The preceding Code directly returns all the node labels in the imported HTML code.

From being a powerful programmer-the path to learning
 

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.