一:首先说一下 Span的各种功能
BackgroundColorSpan 背景色
ClickableSpan 文本可点击,有点击事件
ForegroundColorSpan 文本颜色(前景色)
DrawableMarginSpan 文本插入图片
MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
MetricAffectingSpan 父类,一般不用
StrikethroughSpan 删除线(中划线)
UnderlineSpan 下划线
QuoteSpan 竖线;
AbsoluteSizeSpan 绝对大小(文本字体)
DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
ImageSpan 图片
IconMarginSpan 插入图片偏移
RelativeSizeSpan 相对大小(文本字体)
ReplacementSpan 父类,一般不用
ScaleXSpan 基于x轴缩放
StyleSpan 字体样式:粗体、斜体等
SubscriptSpan 下标(数学公式会用到)
SuperscriptSpan 上标(数学公式会用到)
TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
TypefaceSpan 文本字体
URLSpan 文本超链接
AlignmentSpan.Standard 文本对齐方式
二:废话不多说,使用方法比较简单,一看即懂
1、设置text文本SpannableString spanstr=new SpannableString("锦瑟无端五十弦,一弦一柱思华年。\n" + "庄生晓梦迷蝴蝶,望帝春心托杜鹃。\n" + "沧海月明珠有泪,蓝田日暖玉生烟。\n" + "此情可待成追忆,只是当时已惘然。");`
2、对text文本进行设置
//插入图片 IconMarginSpan
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
spanstr.setSpan(new IconMarginSpan(bitmap,10),0,1,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//图片插入文本 DrawableMarginSpan
spanstr.setSpan(new DrawableMarginSpan(getResources().getDrawable(R.mipmap.ic_launcher_round)),0,1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置背景颜色
spanstr.setSpan(new BackgroundColorSpan(Color.YELLOW),0,3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置字体颜色
spanstr.setSpan(new ForegroundColorSpan(Color.RED),3,8,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置文本可点击,有点击事件
spanstr.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget)<