void setSpan (Object what, int start, int end, int flags)
object what :对应的各种Span,后面会提到;
int start:开始应用指定Span的位置,索引从0开始
int end:结束应用指定Span的位置,不包含
int flags
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE:前后都不包括(在标志位【start,end)前后添加文字,新添加的文字不会有任何设置的属性)
Spannable.SPAN_EXCLUSIVE_INCLUSIVE :前面不包括,后面包括。(在标志位【start,end)前添加文字,新添加的文字不会有任何设置的属性,后边的添加的文字会带有设置的what属性)
Spannable.SPAN_INCLUSIVE_EXCLUSIVE :前面包括,后面不包括。(在标志位【start,end)后添加文字,新添加的文字不会有任何设置的属性,前边边的添加的文字会带有设置的what属性)
Spannable.SPAN_INCLUSIVE_INCLUSIVE :前后都包括。前后都不包括(在标志位【start,end)前后添加文字,新添加的文字会有设置的属性)
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(Color.parseColor("#FF4040"));
stringBuilder.setSpan(foregroundColorSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
//背景颜色
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(Color.parseColor("#FF4040"));
stringBuilder.setSpan(backgroundColorSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
ClickableSpan clickableSpan=new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"可点击",Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
//去掉可点击文字的下划线
ds.setUnderlineText(false);
}
};
//文本可点击,有点击事件
stringBuilder.setSpan(clickableSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 设置此方法后,点击事件才能生效
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
MaskFilter filter=new BlurMaskFilter(4.0f,BlurMaskFilter.Blur.OUTER);
MaskFilterSpan maskFilterSpan=new MaskFilterSpan(filter);
stringBuilder.setSpan(maskFilterSpan,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
stringBuilder.setSpan(new StrikethroughSpan(),0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
UnderlineSpan un=new UnderlineSpan();
//下划线效果
stringBuilder.setSpan(un,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
AbsoluteSizeSpan ab=new AbsoluteSizeSpan(30,true);
//文本字体绝对的大小
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
DynamicDrawableSpan ab=new DynamicDrawableSpan() {
@Override
public Drawable getDrawable() {
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,50,50);
return drawable;
}
};
//文本字体绝对的大小
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher_round);
drawable.setBounds(0,0,100,100);
ImageSpan ab=new ImageSpan(drawable);
//文本字体绝对的大小
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
RelativeSizeSpan ab=new RelativeSizeSpan(3.0f);
//文本字体绝对的大小
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
ScaleXSpan ab=new ScaleXSpan(3.0f);
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
StyleSpan ab=new StyleSpan(Typeface.BOLD_ITALIC);
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="22的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
SuperscriptSpan ab=new SuperscriptSpan();
stringBuilder.setSpan(ab,1,2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
//自定义文本样式
TextAppearanceSpan ab=new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium);
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
//文字字体
TypefaceSpan ab=new TypefaceSpan ("serif");
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
String content="预祝党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
//文字字体
URLSpan ab=new URLSpan ("http://www.baidu.com");
stringBuilder.setSpan(ab,0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(new LinkMovementMethod());
textView.setText(stringBuilder);
String content="log10党的十九大完美谢慕";
SpannableStringBuilder stringBuilder=new SpannableStringBuilder(content);
SubscriptSpan ab=new SubscriptSpan();
stringBuilder.setSpan(ab,3,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(new LinkMovementMethod());
textView.setText(stringBuilder);