SpannableString
• 1 BackgroundColorSpan 背景色
• 2 ClickableSpan 文本可点击
• 3 ForegroundColorSpan 文本颜色
• 4 MaskFilterSpan (BlurMaskFilter) (EmbossMaskFilter) 修饰效果,弱模糊(BlurMaskFilter)
• 5 MetricAffectingSpan 父类 一般不用
• 6 RasterizerSpan 光栅效果
• 7 StrikethroughSpan 删除线
• 8 SuggestionSpan 相当于占位符
• 9 UnderlineSpan 下划线
• 10 AbsoluteSizeSpan 绝对大小(文本字体)
• 11 DynamicDrawableSpan 设置图片 基于文本基线或者底部对齐
• 12 ImageSpan 图片
• 13 RelativeSizeSpan 相对大小(文本字体)
• 14 ReplacementSpan 父类,一般不用
• 15 ScaleXSpan x 基于X轴缩放
• 16 StyleSpan 字体样式:粗体,斜体等
• 17 SubscriptSpan 下标
• 18 SuperscriptSpan 上标
• 19 TextAppearanceSpan 文本外貌(包括字体大小,样式和颜色)
• 20 TypefaceSpan 文本字体
• 21 URLSpan 文本超链接
BackgroundColorSpan 背景色
String content = " 要显示的内容" ;
SpannableString spannableString = new SpannableString(content);
spannableString.setSpan(new BackgroundColorSpan(Color.GRAY),0,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
ForegroundColorSpan 前景色
String content = " 要显示的内容" ;
SpannableString spannableString = new SpannableString(content);
spannableString.setSpan(new ForegroundColorSpan(Color.GRAY),0,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
UnderlineSpan 下划线
String content = " 要显示的内容" ;
SpannableString spannableString = new SpannableString(content); spannableString.setSpan(new UnderlineSpan(),0,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
ImageSpan
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); drawable.setBounds(0,0,80,80);
SpannableString spannableString = new SpannableString(content); spannableString.setSpan(new ImageSpan(drawable),3,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
StyleSpan
SpannableString spannableString = new SpannableString(content);
spannableString.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),3,5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
SubscriptSpan
SpannableString spannableString = new SpannableString(content); spannableString.setSpan(new SubscriptSpan(),3,5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
SuperscriptSpan
SpannableString spannableString = new SpannableString(content); spannableString.setSpan(new SuperscriptSpan(),3,5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
URLSpan
SpannableString spannableString = new SpannableString(content);
spannableString.setSpan(new URLSpan("http://www.baidu.com"),3,5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
/ URLSpan
textView.setMovementMethod(new LinkMovementMethod());
ClickableSpan
SpannableString spannableString = new SpannableString(content);
ClickableSpan clickableSpan = new ClickableSpan() { @Override
public void onClick(View widget) {
System.out.println("widget = " + widget);
}
@Override
public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setColor(Color.RED); ds.setUnderlineText(true);
} } ;
spannableString.setSpan(clickableSpan,3,5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView.setText(spannableString);
/ URLSpan
textView.setMovementMethod(new LinkMovementMethod());