Android TextView use spannablestring to set up the method of composite text _android

Source: Internet
Author: User

This example describes the Android TextView method of using spannablestring to set up composite text. Share to everyone for your reference, specific as follows:

TextView is often used to display plain text, but sometimes you need to make style, event-related settings for some of the text. The Android system uses the Spannablestring class to handle the specified text, with the following features:

1. Backgroundcolorspan Background color
2, Clickablespan text can be clicked, there are click events
3, Foregroundcolorspan text color (foreground color)
4, Maskfilterspan cosmetic effect, such as fuzzy (Blurmaskfilter), Relief (Embossmaskfilter)
5, Metricaffectingspan parent class, generally not
6, Rasterizerspan Grating effect
7, Strikethroughspan strikethrough (underlined)
8, Suggestionspan equivalent to the placeholder
9, Underlinespan underline
10, Absolutesizespan absolute size (text font)
11, Dynamicdrawablespan set the picture, based on the text baseline or bottom alignment.
12, Imagespan pictures
13, Relativesizespan relative size (text font)
14, Replacementspan parent class, generally not
15, Scalexspan based on the x-axis scaling
16, Stylespan font style: bold, italic, etc.
17, Subscriptspan subscript (the mathematical formula will be used)
18, Superscriptspan superscript (the mathematical formula will be used)
19, Textappearancespan text appearance (including font, size, style and color)
20, Typefacespan text font
21, Urlspan text hyperlink

Detailed description is as follows:

1. Backgroundcolorspan Background color

spannablestring spantext = new Spannablestring ("cloud-dwelling community-http://www.jb51.net");
Spantext.setspan (New Backgroundcolorspan (Color.green), 0, Spantext.length (),
spannable.span_inclusive_ EXCLUSIVE);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

2, Clickablespan text can be clicked, there are click events

View: Android using TextView to implement a method without underline hyperlinks

3, Foregroundcolorspan text color (foreground color)

Spantext = new Spannablestring ("cloud-dwelling community-http://www.jb51.net");
Spantext.setspan (New Foregroundcolorspan (Color.Blue), 6, Spantext.length (),
spannable.span_inclusive_ EXCLUSIVE);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

4, Maskfilterspan cosmetic effect, such as fuzzy (Blurmaskfilter), Relief (Embossmaskfilter)

Spantext = new Spannablestring ("Maskfilterspan-http://www.jb51.net");
int length = Spantext.length ();
Blur (Blurmaskfilter)
maskfilterspan maskfilterspan = new Maskfilterspan (New Blurmaskfilter (3, Blur.outer));
Spantext.setspan (Maskfilterspan, 0, Length-10, spannable.
span_inclusive_exclusive);
Embossed (embossmaskfilter)
Maskfilterspan = new Maskfilterspan (new Embossmaskfilter (New float[]{1,1,3}, 1.5f, 8, 3)); C8/>spantext.setspan (Maskfilterspan, length-10, length, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: From the effect of the picture, the relief effect is not obvious. Set the font to a larger point and you can see it clearly. Additional effects are required to inherit maskfilter from the definition.

6, Rasterizerspan Grating effect

Spantext = new Spannablestring ("Strikethroughspan");
Spantext.setspan (New Strikethroughspan (), 0, 7, spannable.
span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: Temporarily unclear, the effect is not obvious.

7, Strikethroughspan strikethrough (underlined)

Spantext = new Spannablestring ("Strikethroughspan");
Spantext.setspan (New Strikethroughspan (), 0, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

8, Suggestionspan

Equivalent to placeholders, typically used in edittext input boxes. When you double-click the text, a prompt box pops up to select some suggested text, and the selected text replaces this placeholder. In the input method for more.

Ps:api 14 New, no samples.

9, Underlinespan underline

Spantext = new Spannablestring ("Underlinespan");
Spantext.setspan (New Underlinespan (), 0, Spantext.length (),
spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

10, Absolutesizespan absolute size (text font)

Spantext = new Spannablestring ("Absolutesizespan");
Spantext.setspan (New Absolutesizespan (True), 0, Spantext.length (),
spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: The size of the font set large, the effect of Hale.

11, Dynamicdrawablespan set the picture, based on the text baseline or bottom alignment.

Dynamicdrawablespan Drawablespan =
 new Dynamicdrawablespan (dynamicdrawablespan.align_baseline) {
 @Override Public
 drawable getdrawable () {
  drawable d = getresources (). getdrawable (R.drawable.ic_launcher);
  D.setbounds (0, 0, m);
  return D;
 }
;
Dynamicdrawablespan drawableSpan2 = new Dynamicdrawablespan (
dynamicdrawablespan.align_bottom) {
 @ Override public
 drawable getdrawable () {
   drawable d = getresources (). getdrawable (R.drawable.ic_launcher);
   D.setbounds (0, 0, m);
    return D;
   }
  ;
Spantext.setspan (Drawablespan, 3, 4, spannable.span_inclusive_exclusive);
Spantext.setspan (drawableSpan2, 7, 8, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: The left image is based on the baseline alignment, the right picture is based on the bottom alignment

12, Imagespan pictures

Spantext = new Spannablestring ("Imagespan");
drawable d = getresources (). getdrawable (R.drawable.ic_launcher);
D.setbounds (0, 0, m);
Spantext.setspan (New Imagespan (d), 3, 4, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: And Dynamicdrawablespan is not very different

13, Relativesizespan relative size (text font)

Spantext = new Spannablestring ("Relativesizespan");
Parameter proportion: Proportional size
Spantext.setspan (new Relativesizespan (2.5f), 3, 4,
spannable.span_inclusive_ EXCLUSIVE);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: The relative size ratio is based on the current text font size

15, Scalexspan based on the x-axis scaling

Spantext = new Spannablestring ("Scalexspan--cloud-dwelling community");
Parameter proportion: Proportional size
Spantext.setspan (new Scalexspan (3.8f), 3, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

16, Stylespan font style: bold, italic, etc.

Spantext = new Spannablestring ("Stylespan--cloud-dwelling community");
Typeface.bold_italic: Bold + italic
Spantext.setspan (new Stylespan (Typeface.bold_italic), 3, 7,
Spannable.span_ inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

17, Subscriptspan subscript (the mathematical formula will be used)

Spantext = new Spannablestring ("Subscriptspan--cloud-dwelling community");
Spantext.setspan (New Subscriptspan (), 6, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

18, Superscriptspan superscript (the mathematical formula will be used)

Spantext = new Spannablestring ("Superscriptspan--cloud-dwelling community");
Spantext.setspan (New Superscriptspan (), 6, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

19, Textappearancespan text appearance (including font, size, style and color)

Spantext = new Spannablestring ("Textappearancespan--cloud-dwelling community");
If you want to customize Textappearance, you can modify the system style
Spantext.setspan (this, Android. R.style.textappearance_medium),
 6, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: The system also provides related values Textappearance_small, Textappearance_large, etc. If necessary, it can be modified on the basis of the above style.

20, Typefacespan text font

Spantext = new Spannablestring ("Typefacespan--cloud-dwelling community");
If you want to use a custom font, you may want to override the class Typefacespan
spantext.setspan ("Monospace"), 3,
Spannable.span_ inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);

PS: About custom font settings, later on how to use the

21, Urlspan text hyperlink

Spantext = new Spannablestring ("Urlspan--cloud-dwelling community");
Spantext.setspan (New Urlspan ("Http://www.jb51.net"), Spantext.length (),
spannable.span_inclusive_ EXCLUSIVE);
Mtvtext.append ("\ n");
Mtvtext.append (spantext);
Allow Urlspan to click
mtvtext.setmovementmethod (New Linkmovementmethod ());

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.