Android TextView Text spannablestring Set Property usage examples _android

Source: Internet
Author: User
Tags set background

This example describes the way in which text in the Android TextView sets properties through Spannablestring. Share to everyone for your reference, specific as follows:

In Android, TextView is the most common control we use to display text.

In general, the text in TextView is a style. So how do you set the font, size, color, style, and hyperlink properties for the text of each part of the TextView? Let's demonstrate this by spannablestring the specific example of the operation.

Create a Spannablestring object spannablestring MSP = new Spannablestring ("
Font test font size half twice times foreground background color normal bold italic bold italic underline delete line x1x2 phone message website SMS MMS map X-axis synthesis "); Set Font (DEFAULT,DEFAULT-BOLD,MONOSPACE,SERIF,SANS-SERIF) msp.setspan (New Typefacespan ("Monospace"), 0, 2,
spanned.span_exclusive_exclusive);
Msp.setspan (New Typefacespan ("serif"), 2, 4, spanned.span_exclusive_exclusive);
Sets the font size (absolute, Unit: pixel) Msp.setspan (new Absolutesizespan, 4, 6, spanned.span_exclusive_exclusive); Msp.setspan (New Absolutesizespan (20,true), 6, 8, spanned.span_exclusive_exclusive);
The second parameter, Boolean dip, if true, indicates that the front font size unit is dip, otherwise pixels, ditto. Set the font size (relative value, Unit: pixel) parameter is expressed as the default font size Msp.setspan (new Relativesizespan (0.5f), 8, spanned.span_exclusive_exclusive) ; 0.5f represents the default font size of half Msp.setspan (new Relativesizespan (2.0f), ten, spanned.span_exclusive_exclusive); 2.0f indicates a default font size of twice times//Set font foreground color Msp.setspan (new Foregroundcolorspan (Color.magenta), spanned.span_exclusive_ EXCLUSIVE); Set foreground color to magenta//Set font background color Msp.setspan (new Backgroundcolorspan (Color.cyan), 15, 18, spanned.span_exclusive_exclusive); Set background color to cyan//Set font style normal, bold, italic, Bold italic Msp.setspan (new Stylespan (Android.graphics.Typeface.NORMAL), Spanned.span_ exclusive_exclusive); Normal Msp.setspan (new Stylespan (Android.graphics.Typeface.BOLD), spanned.span_exclusive_exclusive); Bold Msp.setspan (New Stylespan (Android.graphics.Typeface.ITALIC), spanned.span_exclusive_exclusive); Italic Msp.setspan (New Stylespan (Android.graphics.Typeface.BOLD_ITALIC), N, spanned.span_exclusive_exclusive);
Bold Italic//Set underline Msp.setspan (new Underlinespan (), M, spanned.span_exclusive_exclusive);
Set the Strikethrough Msp.setspan (new Strikethroughspan (), spanned.span_exclusive_exclusive);   Set up superscript msp.setspan (new Subscriptspan (), spanned.span_exclusive_exclusive);  Subscript Msp.setspan (New Superscriptspan (), N, Panax Notoginseng, spanned.span_exclusive_exclusive); Superscript//hyperlink (need to add Setmovementmethod method additional response) Msp.setspan (New Urlspan ("tel:4155551212"), Panax Notoginseng, spanned.span_exclusive_   EXCLUSIVE); Telephone Msp.setspan (NEW Urlspan ("mailto:webmaster@google.com"), spanned.span_exclusive_exclusive);   Mail Msp.setspan (New Urlspan ("http://www.baidu.com"), A., spanned.span_exclusive_exclusive);   Network Msp.setspan (New Urlspan ("sms:4155551212"), spanned.span_exclusive_exclusive);   SMS used in SMS: or Smsto:msp.setSpan (New Urlspan ("mms:4155551212"), A., spanned.span_exclusive_exclusive);   MMS using MMS: or Mmsto:msp.setSpan (New Urlspan ("geo:38.899533,-77.036476"), the ",", spanned.span_exclusive_exclusive); Map//Set font size (relative value, Unit: pixel) parameter is expressed as the default font width of how many times Msp.setspan (new Scalexspan (2.0f),,,, spanned.span_exclusive_exclusive); 2.0F represents twice times the default font width, which is twice times the x-axis font, and the height is unchanged/set bullets Msp.setspan (New Bulletspan ( Android.text.style.bulletspan.standard_gap_width,color.green), 0, spanned.span_exclusive_exclusive);
The first parameter represents the width that the bullet occupies, and the second parameter is the bullet Color tv_textview.settext (MSP);

 Tv_textview.setmovementmethod (Linkmovementmethod.getinstance ());

The effect chart is as follows:

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.