Combination of Android TextView and Html

Source: Internet
Author: User

TextView in Android supports some Html format tags. This includes common font size and color settings and text links. It is easy to use. You only need to use Html class conversion. For example:
TextView. setText (Html. fromHtml (str ));
I. Implement TextView text with different colors
[Plain]
Import android. text. Html;

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. "));
Ii. TextView: display images in html files
We know that we want TextView to parse and display Html code. Available
[Plain]
Spanned text = Html. fromHtml (source );
TV. setText (text );
This is easy to use.
However, how can we make TextView display the image of the <image> node in Html?
We can see that fromHtml has another refactoring:
FromHtml (String source, Html. ImageGetter imageGetter, Html. TagHandler tagHandler)
Implement ImageGetter to display the image:
[Plain]
ImageGetter imgGetter = new Html. ImageGetter (){
@ Override
Public Drawable getDrawable (String source ){
Drawable drawable = null;
Drawable = Drawable. createFromPath (source); // Or fetch it from the URL
// Important
Drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable
. GetIntrinsicHeight ());
Return drawable;
}
};
As for TagHandler, we do not need to use it here. You can directly transmit null.
 

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.