Android spannablestringbuilder Usage CollationCategory: Android development 2013-11-29 10:58 5009 people read reviews (0) favorite reports Androidspannablestringbuild
Spannablestringbuilder usage:
spannablestring ss = new Spannablestring ("Red call italics strikethrough green underline Picture:.");
Mark text with color
Ss.setspan (New Foregroundcolorspan (color.red), 0, 2,
Setspan required Flag,spanned.span_exclusive_exclusive (not included before and after).
spanned.span_exclusive_exclusive);
Mark text with a hyperlink
Ss.setspan (New Urlspan ("tel:4155551212"), 2, 5,
spanned.span_exclusive_exclusive);
Mark text with style (italic)
Ss.setspan (New Stylespan (Typeface.bold_italic), 5, 7,
spanned.span_exclusive_exclusive);
Mark text with strikethrough
Ss.setspan (New Strikethroughspan (), 7, 10,
spanned.span_exclusive_exclusive);
Mark text with an underscore
Ss.setspan (New Underlinespan (), 10, 16,
spanned.span_exclusive_exclusive);
Mark with color
Ss.setspan (New Foregroundcolorspan (Color.green), 10, 13,
spanned.span_exclusive_exclusive);
Get drawable Resources
drawable d = getresources (). getdrawable (R.drawable.icon);
D.setbounds (0, 0, d.getintrinsicwidth (), D.getintrinsicheight ());
Create Imagespan
Imagespan span = new Imagespan (d, Imagespan.align_baseline);
Replace text with Imagespan
Ss.setspan (span,,, spannable.span_inclusive_exclusive);
Txtinfo.settext (ss);
Txtinfo.setmovementmethod (Linkmovementmethod.getinstance ()); Implementing scrolling of text
Usually used to display text, but sometimes also need to be in the text with some pictures, such as QQ can use emoticons, such as the need for text highlighting and so on, how to do so in Android?
Remember that there is a Android.text package in Android, which provides powerful handling of the text.
Add pictures primarily with spannablestring and Imagespan classes:
drawable drawable = Getresources (). getdrawable (ID);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
The text that needs to be processed, [smile] is the text that needs to be substituted
spannablestring spannable = new Spannablestring (GetText (). toString () + "[Smile]");
To replace the specified text with a picture, you need to use Imagespan
Imagespan span = new Imagespan (drawable, imagespan.align_baseline);
Start replacing, note that the 2nd and 3rd parameters indicate where to start replacing where to replace end (start and end)
The last parameter is similar to the set in mathematics, [5,12] represents from 5 to 12, including 5 but excluding 12
Spannable.setspan (span, GetText (). Length (), GetText (). Length () + "[smile]". Length (), spannable.span_inclusive_ EXCLUSIVE);
SetText (spannable);
Highlight the text you want:
public void Highlight (int start,int end) {
Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());//For variable strings
Foregroundcolorspan span=new Foregroundcolorspan (color.red);
Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);
}
Add Underline:
public void underline (int start,int end) {
Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());
Characterstyle span=new Underlinespan ();
Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);
}
Combined use:
Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ());
Characterstyle span_1=new Stylespan (Android.graphics.Typeface.ITALIC);
Characterstyle span_2=new Foregroundcolorspan (color.red);
Spannable.setspan (span_1, start, end, spannable.span_exclusive_exclusive);
Spannable.setspan (span_2, start, end, spannable.span_exclusive_exclusive);
SetText (spannable);
Case: a string with \ n line break can display 2 colors in this method
/**
* string with \ n line break can display 2 colors in this method
* @param text
* @param color1
* @param color2
* @return
*/
Public Spannablestringbuilder Highlight (String text,int color1,int color2,int fontSize) {
Spannablestringbuilder spannable=new Spannablestringbuilder (text);//For variable strings
Characterstyle span_0=null,span_1=null,span_2;
int End=text.indexof ("\ n");
if (end==-1) {//display with the first color if there is no line break
Span_0=new Foregroundcolorspan (Color1);
Spannable.setspan (span_0, 0, Text.length (), spannable.span_exclusive_exclusive);
}else{
Span_0=new Foregroundcolorspan (Color1);
Span_1=new Foregroundcolorspan (COLOR2);
Spannable.setspan (span_0, 0, end, spannable.span_exclusive_exclusive);
Spannable.setspan (span_1, End+1, Text.length (), spannable.span_exclusive_exclusive);
Span_2=new Absolutesizespan (fontSize);//font size
Spannable.setspan (Span_2, End+1, Text.length (), spannable.span_exclusive_exclusive);
}
return spannable;
}
Android Spannablestringbuilder Usage Collation