Textview displays HTML-class parsed webpages and images and custom tags

Source: Internet
Author: User
The best control for displaying HTML web pages in Android is webview. Sometimes, to meet specific requirements, you need to display HTML web pages, images, and custom resolution tags in textview. 1. textview displays the HTML-class parsing webpage charsequence richtext = html. fromhtml ("<strong> radish cabbage blog </strong> -- <a href = 'HTTP: // orgcent.com '> http://orgcent.com </a>"); mtvtext. settext (richtext); // This line is required. Otherwise, the hyperlink cannot be clicked. scrollingmovementmethod implements the scroll bar mtvtext. setmovementmethod (linkmovementmethod. getinstance (); PS: If you want to make the content scroll and hyperlink at the same time, you only need to set linkmovementmethod. Because it inherits scrollingmovementmethod. About scrollingmovementmethod, you can view the vertical or horizontal scrolling of textview implemented by Android 2. textview displays HTML-parsed images and custom tags final string html = "radish cabbage blog <mytag color = 'Blue '> Custom </mytag> "; // process unknown tags, which are usually final HTML tags that cannot be processed by the system by default. taghandler = new HTML. taghandler () {int contentindex = 0;/*** opening: whether it is the start tag * tag: Tag name * output: output information, used to save the processed information * xmlreader: reads information about the current tag, such as attributes. */Public void handletag (Boolean opening, string tag, editable output, xmlreader) {If ("mytag ". equals (TAG) {If (opening) {// obtain the content starting position of the current tag contentindex = output. length (); try {final string color = (string) xmlreader. getproperty ("color");} catch (exception e) {e. printstacktrace () ;}} else {final int length = output. length (); string content = output. subsequence (contentindex, length ). tostring (); spannablestring spanstr = new spannablestring (content); spanstr. setspan (New foregroundcolorspan (color. green), 0, content. length (), spannable. span_inclusive_exclusive); output. replace (contentindex, length, spanstr) ;}} system. out. println ("Opening:" + opening + ", Tag:" + tag + ", output:" + output) ;}; // parse the final HTML image. imagegetter = new HTML. imagegetter () {public drawable getdrawable (string source) {// The image drawable D must be asynchronously loaded here = NULL; try {inputstream is = new defaulthttpclient(cmd.exe cute (New httpget (source )). getentity (). getcontent (); bitmap Bm = bitmapfactory. decodestream (is); D = new bitmapdrawable (BM); // setbounds (0, 0, BM. getwidth (), BM. getheight (); D. setbounds (0, 0,200,300);} catch (exception e) {e. printstacktrace () ;}return D ;}; richtext = html. fromhtml (HTML, imagegetter, taghandler); mtvtext. settext (richtext );

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.