Use spannablestring in textview to set composite text

Source: Internet
Author: User

Textview is usually used to display common text, but sometimes you need to set the style and event of some of the text. Android systemSpannablestringClass to process the specified text. The specific functions are as follows:

1. backgroundcolorspan background color
2. clickablespan text clickable, with click events
3. foregroundcolorspan text color (foreground color)
4. maskfilterspan modifier effects, such as blurmaskfilter and embossmaskfilter)
5. metricaffectingspan parent class, generally not used
6. rasterizerspan grating effect
7. strikethroughspan strikethrough)
8. suggestionspan is equivalent to a placeholder
9. underlinespan underline
10. absolutesizespan absolute size (text font)
11. Set the Image Based on the text baseline or bottom alignment in dynamicdrawablespan.
12. imagespan Image
13. Relative relativesizespan size (text font)
14. replacementspan parent class, generally not used
15. scalexspan scales based on the X axis
16. stylespan Font Style: bold and italic
17. subscriptspan subscript (used in mathematical formulas)
18. superscriptspan tagging (used in mathematical formulas)
19. textappearancespan text appearance (including font, size, style, and color)
20. typefacespan text font
21. urlspan text hyperlink

So many words, in order to give you the courage to read them, let's look at the following:

1. backgroundcolorspan background color

Spannablestring spantext = new spannablestring ("radish cabbage blog-http://orgcent.com"); spantext. setspan (New backgroundcolorspan (color. green), 0, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

2. clickablespan text clickable, with click events
View: Android uses textview for underline-free hyperlinks

3. foregroundcolorspan text color (foreground color)

Spantext = new spannablestring ("http://orgcent.com"); spantext. setspan (New foregroundcolorspan (color. blue), 6, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

4. maskfilterspan modifier effects, such as blurmaskfilter and embossmaskfilter)

Spantext = new spannablestring ("maskfilterspan -- http://orgcent.com"); int length = spantext. length (); // blur (blurmaskfilter) maskfilterspan = new maskfilterspan (New blurmaskfilter (3, blur. outer); spantext. setspan (maskfilterspan, 0, length-10, spannable. span_inclusive_exclusive); // embossmaskfilter maskfilterspan = new maskfilterspan (New embossmaskfilter (new float [] {, 3}, 1.5f, 8, 3); spantext. setspan (maskfilterspan, length-10, length, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

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: it is unclear, but the effect is not obvious.

7. strikethroughspan strikethrough)

spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(),0,7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

8. suggestionspan
It is equivalent to a placeholder and is generally used in the edittext input box. When you double-click the text, a prompt box will pop up to select some recommended text. The selected text will replace the placeholder. Most of the input methods are used.
PS: API 14 is added. No example is provided.

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(20,true),0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

PS: Set the font size to a greater value, and the effect is tough.

11. Set the Image Based on the text baseline or bottom alignment in dynamicdrawablespan.

DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE){    @Override    public Drawable getDrawable(){        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);        d.setBounds(0,0,50, 50);        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,50, 50);                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: Align the left image with the baseline, and align the right image with the bottom image.

12. imagespan Image

spanText = new SpannableString("ImageSpan");Drawable d = getResources().getDrawable(R.drawable.ic_launcher);</strong>d.setBounds(0,0,50, 50);spanText.setSpan(new ImageSpan(d),3,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

PS: Not much different from dynamicdrawablespan

13. Relative relativesizespan 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 is based on the current text font size.

15. scalexspan scales based on the X axis

Spantext = new spannablestring ("scalexspan -- radish cabbage blog"); // parameter proportion: proportion size spantext. setspan (New scalexspan (3.8f), 3, 7, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

16. stylespan Font Style: bold and italic

Spantext = new spannablestring ("stylespan -- radish cabbage blog"); // 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 (used in mathematical formulas)

Spantext = new spannablestring ("subscriptspan -- radish cabbage blog"); spantext. setspan (New subscriptspan (), 6,7, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

18. superscriptspan tagging (used in mathematical formulas)

Spantext = new spannablestring ("superscriptspan -- radish cabbage blog"); 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 -- radish cabbage blog"); // to customize textappearance, you can modify the spantext in the system style. setspan (New textappearancespan (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 and textappearance_large. You can modify the above style as needed.

20. typefacespan text font

Spantext = new spannablestring ("typefacespan -- radish cabbage blog"); // to use a Custom font, you may need to override the typefacespanspantext class. setspan (New typefacespan ("monospace"), 3,10, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

PS: For how to set a Custom font, we will introduce how to use it later.

21. urlspan text hyperlink

Spantext = new spannablestring ("urlspan -- radish cabbage blog"); spantext. setspan (New urlspan ("http://orgcent.com"), 10, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext); // enables urlspan to click mtvtext. setmovementmethod (New linkmovementmethod ());

PS:
1. linkmovementmethod inherits the scrollingmovementmethod class. This class is used to implement textview scrolling without labels. For details, see: Android implements vertical or horizontal scrolling of textview.
2. If you want to achieve a hyperlink without underline, you can use textview to achieve a hyperlink without underline.

Transferred from:
Http://blog.csdn.net/perley/article/details/8127568/ | radish cabbage blog

 

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.