Android_textview_richtextview (rich text)

Source: Internet
Author: User

How to set different font styles in the same textview?

Required class: Android. Text. spanned;

Android. Text. spannablestring;

Android. Text. spannablestringbuilder

The difference between spannablestring and spannablestringbuilder is equivalent to the difference between string and stringbuffer.

Spannablestring and spannablestringbuilder can be used to set different spans. These spans are used to implement rich textview, such as bold, italic, foreground color, background color, font size, and font style. Android. text. style. * defines many span types for use.

For the common span, see the demo below.

The flags in setspan (object what, int start, int end, int flags) can be set to 0 or the value defined in spanned.

  • Spanned. span_exclusive_exclusive --- does not contain the endpoints of start and end at both ends
  • Spanned. span_exclusive_inclusive --- does not contain start, but includes end
  • Spanned. span_inclusive_exclusive --- contains start, but does not include end
  • Spanned. span_inclusive_inclusive --- contains the endpoint of start and end

Where 0 is equivalent to spanned. span_inclusive_inclusive

The package here is like the open and closed intervals in mathematics.

Below is a demo

Package COM. tianjf; import android. app. activity; import android. graphics. color; import android. graphics. drawable. drawable; import android. OS. bundle; import android. text. spannablestring; import android. text. spanned; import android. text. method. linkmovementmethod; import android. text. style. absolutesizespan; import android. text. style. backgroundcolorspan; import android. text. style. bulletspan; import android. text. style. foregroundcolorspan; import android. text. style. imagespan; import android. text. style. relativesizespan; import android. text. style. scalexspan; import android. text. style. strikethroughspan; import android. text. style. stylespan; import android. text. style. subscriptspan; import android. text. style. superscriptspan; import android. text. style. typefacespan; import android. text. style. urlspan; import android. text. style. underlinespan; import android. widget. textview; public class richtextviewdemoactivity extends activity {textview mtextview = NULL; spannablestring mspannablestring = NULL; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtextview = (textview) findviewbyid (R. id. mytextview ); // create a spannablestring object mspannablestring = new spannablestring ("font test font size half twice foreground color background color normal bold italic underline strikethrough x1x2 Phone Mail website sms mms Map x axis/BOT "); // set the font (default, default-bold, monospace, Serif, sans-serif) mspannablestring. setspan (New typefacespan ("monospace"), 0, 2, spanned. span_exclusive_exclusive); mspannablestring. setspan (New typefacespan ("Sans-serif"), 2, 4, spanned. span_exclusive_exclusive); // set the font size (absolute value, unit: pixel) mspannablestring. setspan (New absolutesizespan (20), 4, 8, spanned. span_exclusive_exclusive); // set the font size (relative value, unit: pixels). The parameter indicates how many times the default font width is mspannablestring. setspan (New scalexspan (2.0f), 49, 51, spanned. span_exclusive_exclusive); // 2.0f indicates twice the default font width, that is, to enlarge the X axis to twice the default font width, while the height remains unchanged. // set the font size (relative value, unit: pixel) parameter indicates how many times the default font size mspannablestring. setspan (New relativesizespan (0.5f), 8, 10, spanned. span_exclusive_exclusive); // 0.5f indicates half of the default font size mspannablestring. setspan (New relativesizespan (2.0f), 10, 12, spanned. span_exclusive_exclusive); // 2.0f indicates twice the default font size. // set the foreground color of the font mspannablestring. setspan (New foregroundcolorspan (color. magenta), 12,15, spanned. span_exclusive_exclusive); // set the foreground color to magenta // set the font background color to mspannablestring. setspan (New backgroundcolorspan (color. cyan), 15, 18, spanned. span_exclusive_exclusive); // set the background color to cyan // set the font style to normal, bold, italic, and bold italic mspannablestring. setspan (New stylespan (Android. graphics. typeface. normal), 18, 20, spanned. span_exclusive_exclusive); // normal mspannablestring. setspan (New stylespan (Android. graphics. typeface. bold), 20, 22, spanned. span_exclusive_exclusive); // specifies the mspannablestring in bold. setspan (New stylespan (Android. graphics. typeface. italic), 22, 24, spanned. span_exclusive_exclusive); // mspannablestring in italic. setspan (New stylespan (Android. graphics. typeface. bold_italic), 24, 27, spanned. span_exclusive_exclusive); // bold italic // set the underline mspannablestring. setspan (New underlinespan (), 27, 30, spanned. span_exclusive_exclusive); // you can specify whether to delete mspannablestring. setspan (New strikethroughspan (), 30, 33, spanned. span_exclusive_exclusive); // sets up or down the mspannablestring. setspan (New subscriptspan (), 34, 35, spanned. span_exclusive_exclusive); // subscript mspannablestring. setspan (New superscriptspan (), 36, 37, spanned. span_exclusive_exclusive); // superscript // hyperlink (you need to add the setmovementmethod Method to the response) mspannablestring. setspan (New urlspan ("Tel: 4155551212"), 37, 39, spanned. span_exclusive_exclusive); // phone mspannablestring. setspan (New urlspan ("mailto: webmaster@google.com"), 39, 41, spanned. span_exclusive_exclusive); // email mspannablestring. setspan (New urlspan ("http://www.baidu.com"), 41, 43, spanned. span_exclusive_exclusive); // network mspannablestring. setspan (New urlspan ("SMS: 4155551212"), 43, 45, spanned. span_exclusive_exclusive); // SMS: Or smsto: mspannablestring. setspan (New urlspan ("MMS: 4155551212"), 45, 47, spanned. span_exclusive_exclusive); // MMS: Or mmsto: mspannablestring. setspan (New urlspan ("Geo: 38.899533,-77.036476"), spanned. span_exclusive_exclusive); // map // set the project symbol // The first parameter indicates the width occupied by the Project symbol, and the second parameter indicates the project color mspannablestring. setspan (New bulletspan (Android. text. style. bulletspan. standard_gap_width, color. green), 0, mspannablestring. length (), spanned. span_exclusive_exclusive); // set the image drawable = getresources (). getdrawable (R. drawable. ic_launcher); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); mspannablestring. setspan (New imagespan (drawable), 51, 55, spanned. span_exclusive_exclusive); mtextview. settext (mspannablestring); mtextview. setmovementmethod (linkmovementmethod. getinstance ());}}

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.