TextView is often used to display plain text, but sometimes you need to make style, event-related settings for some of the text. Android system through the Spannablestring class to the specified text related processing, the actual application of more places such as chat show expression Ah, friends circle or community in the topic of the display, @ Friends display and click, etc., the keyword display different colors ...
1. Backgroundcolorspan Background color
spannablestring spantext = new Spannablestring ("Backgroundcolorspan");
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
Spannablestring.setspan (New Clickablespan () {
@Override public
void OnClick (View widget) {
Spanclicklistener.onspanclick (bean);
@Override public
void Updatedrawstate (Textpaint ds) {
super.updatedrawstate (DS);
Set Brush Property
Ds.setunderlinetext (FALSE);//default Underline
}
}, Matcher.start (), End, spannable.span_exclusive_ EXCLUSIVE);
Tvtopic.settext (spannablestring);
If you want to achieve click, you must set up this
Tvtopic.setmovementmethod (Linkmovementmethod.getinstance ());
3, Foregroundcolorspan text color (foreground color)
Spantext = new Spannablestring ("This is Foregroundcolorspan, see?");
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 ("This is Maskfilterspan, see?");
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);
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.
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);
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);
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);
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);
14, Scalexspan based on the x-axis scaling
Spantext = new Spannablestring ("Scalexspan");
Parameter proportion: Proportional size
Spantext.setspan (new Scalexspan (3.8f), 3, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);
15, Stylespan font style: bold, italic, etc.
Spantext = new Spannablestring ("Stylespan");
Spantext.setspan (New Stylespan (Typeface.bold_italic), 3, 7,
spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);
16, Subscriptspan subscript (the mathematical formula will be used)
Spantext = new Spannablestring ("Subscriptspan");
Spantext.setspan (New Subscriptspan (), 6, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);
17, Superscriptspan superscript (the mathematical formula will be used)
Spantext = new Spannablestring ("Superscriptspan");
Spantext.setspan (New Superscriptspan (), 6, 7, spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);
18, Textappearancespan text appearance (including font, size, style and color)
Spantext = new Spannablestring ("Textappearancespan");
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);
19, Typefacespan text font
Spantext = new Spannablestring ("Typefacespan");
Spantext.setspan (New Typefacespan ("Monospace"), 3,
spannable.span_inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (Spantext);
20, Urlspan text hyperlink
Spantext = new Spannablestring ("Urlspan");
Spantext.setspan (New Urlspan ("http://blog.csdn.net/u011102153"), Spantext.length (),
Spannable.span_ inclusive_exclusive);
Mtvtext.append ("\ n");
Mtvtext.append (spantext);
Allow Urlspan to click
mtvtext.setmovementmethod (New Linkmovementmethod ());
Download: Https://github.com/LineChen/SpannableStringDemo
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.