void setSpan (Object what, int start, int end, int flags)
参数说明:
object what :对应的各种Span
int start:开始应用指定Span的位置,索引从0开始
int end:结束应用指定Span的位置,特效并不包括这个位置。比如如果这里数为3(即第4个字符),第4个字符不会有任何特效。
int flags:取值有如下四个
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE:前后都不包括,即在指定范围的前面和后面插入新字符都不会应用新样式
Spannable.SPAN_EXCLUSIVE_INCLUSIVE :前面不包括,后面包括。即仅在范围字符的后面插入新字符时会应用新样式
Spannable.SPAN_INCLUSIVE_EXCLUSIVE :前面包括,后面不包括。
Spannable.SPAN_INCLUSIVE_INCLUSIVE :前后都包括。
private void addspannablestringbuilderSpan() {
SpannableStringBuilder ss=newSpannableStringBuilder("xxxxxxxxxxxxxxxxxxxx");
//用颜色标记文本
ss.setSpan(newForegroundColorSpan(Color.RED),0,2,,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//用超链接标记文本
ss.setSpan(newURLSpan("tel:4155551212"),2,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//用样式标记文本(斜体)
ss.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),5,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//用删除线标记文本
ss.setSpan(newStrikethroughSpan(),7,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//用下划线标记文本
ss.setSpan(newUnderlineSpan(),10,16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//用颜色标记
ss.setSpan(newForegroundColorSpan(Color.GREEN),10,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//获取Drawable资源
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//创建ImageSpan
ImageSpan span =newImageSpan(d, ImageSpan.ALIGN_BASELINE);
//用ImageSpan替换文本
ss.setSpan(span,18,19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.setText(ss);
tv.setMovementMethod(LinkMovementMethod.getInstance());//实现文本的滚动
}
/**
* 光栅效果
*/
privatevoidaddRasterizerSpan() {
SpannableString spanText =newSpannableString("StrikethroughSpan");
spanText.setSpan(newStrikethroughSpan(),0,7, Spannable.
SPAN_INCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanText);
}
/**
* 修饰效果
*/
privatevoidaddmaskfilteSpan() {
SpannableString spanText =newSpannableString("benzlocke6666666");
intlength = spanText.length();
//模糊(BlurMaskFilter)
MaskFilterSpan maskFilterSpan =newMaskFilterSpan(newBlurMaskFilter(3, Blur.OUTER));
spanText.setSpan(maskFilterSpan,0, length -10, Spannable.
SPAN_INCLUSIVE_EXCLUSIVE);
//浮雕(EmbossMaskFilter)
maskFilterSpan =newMaskFilterSpan(newEmbossMaskFilter(newfloat[]{1,1,3},1.5f,8,3));
spanText.setSpan(maskFilterSpan, length -10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanText);
}
/**
* 超链接
*/
privatevoidaddUrlSpan() {
SpannableString spanString =newSpannableString("超链接");
URLSpan span =newURLSpan("tel:0123456789");
spanString.setSpan(span,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
tv.setMovementMethod(LinkMovementMethod.getInstance());
/**
* 文字背景颜色
*/
privatevoidaddBackColorSpan() {
SpannableString spanString =newSpannableString("文字背景颜色");
BackgroundColorSpan span =newBackgroundColorSpan(Color.YELLOW);
spanString.setSpan(span,0,6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 文字颜色
*/
privatevoidaddForeColorSpan() {
SpannableString spanString =newSpannableString("文字颜色");
ForegroundColorSpan span =newForegroundColorSpan(Color.BLUE);
spanString.setSpan(span,0,4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 字体大小
*/
privatevoidaddFontSpan() {
SpannableString spanString =newSpannableString("36号字体");
AbsoluteSizeSpan span =newAbsoluteSizeSpan(36);
spanString.setSpan(span,0,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 粗体,斜体
*/
privatevoidaddStyleSpan() {
SpannableString spanString =newSpannableString("ABCDEF");
StyleSpan span =newStyleSpan(Typeface.BOLD_ITALIC);
spanString.setSpan(span,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 删除线
*/
privatevoidaddStrikeSpan() {
SpannableString spanString =newSpannableString("删除线");
StrikethroughSpan span =newStrikethroughSpan();
spanString.setSpan(span,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 下划线
*/
privatevoidaddUnderLineSpan() {
SpannableString spanString =newSpannableString("下划线");
UnderlineSpan span =newUnderlineSpan();
spanString.setSpan(span,0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
/**
* 图片
*/
privatevoidaddImageSpan() {
SpannableString spanString =newSpannableString(" ");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span =newImageSpan(d, ImageSpan.ALIGN_BASELINE);
spanString.setSpan(span,0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append("\n");
tv.append(spanString);
}
}