Span 对于android初学者来讲显得有点陌生,但却拥有很多强大的功能,本文以TextView为例,文中错误之处,还望高手指出。先上效果图:
一:首先说一下 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) { Toast.makeText(getApplicationContext(),"锦瑟",Toast.LENGTH_SHORT).show(); } },8,12,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //要相应点击事件必须加上这一步 tv_span.setMovementMethod(LinkMovementMethod.getInstance()); // 修饰效果,模糊BlurMaskFilter 第一个参数 为模糊度,数越大越模糊 ,第二个数为样式 BlurMaskFilter filterINNER=new BlurMaskFilter(10,BlurMaskFilter.Blur.INNER); spanstr.setSpan(new MaskFilterSpan(filterINNER),12,14,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); BlurMaskFilter filterNORMAL=new BlurMaskFilter(1,BlurMaskFilter.Blur.NORMAL); spanstr.setSpan(new MaskFilterSpan(filterNORMAL),14,18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); BlurMaskFilter filterOUTER=new BlurMaskFilter(10,BlurMaskFilter.Blur.OUTER); spanstr.setSpan(new MaskFilterSpan(filterOUTER),18,21,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); BlurMaskFilter filterSOLID=new BlurMaskFilter(10,BlurMaskFilter.Blur.SOLID); spanstr.setSpan(new MaskFilterSpan(filterSOLID),21,24,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //浮雕效果 EmbossMaskFilter EmbossMaskFilter filter=new EmbossMaskFilter(new float[]{20,20,20},0.5f,1,10); spanstr.setSpan(new MaskFilterSpan(filter),24,27,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //删除线(中划线)StrikethroughSpan spanstr.setSpan(new StrikethroughSpan(),27,29,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //下划线 UnderlineSpan spanstr.setSpan(new UnderlineSpan(),29,32,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //竖线 QuoteSpan spanstr.setSpan(new QuoteSpan(Color.RED),32,33,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //绝对大小 AbsoluteSizeSpan spanstr.setSpan(new AbsoluteSizeSpan(40),33,36,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置图片,基于文本基线或底部对齐 DynamicDrawableSpan //new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM)也可以直接写成new DynamicDrawableSpan() DynamicDrawableSpan drawableSpan=new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) { @Override public Drawable getDrawable() { Drawable drawable=getResources().getDrawable(R.drawable.icon); drawable.setBounds(0,0,50,50); return drawable; } }; spanstr.setSpan(drawableSpan,36,37,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //图片 ImageSpan ImageSpan imageSpan=new ImageSpan(getResources().getDrawable(R.drawable.icon)); spanstr.setSpan(imageSpan,38,40,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //字体相对大小 RelativeSizeSpan spanstr.setSpan(new RelativeSizeSpan(2f),40,43,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //基于X轴缩放 ScaleXSpan spanstr.setSpan(new ScaleXSpan(2f),43,45,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //字体样式,粗体 BOLD、斜体 ITALIC等 StyleSpan spanstr.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),45,50,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //下标 SubscriptSpan spanstr.setSpan(new SubscriptSpan(),50,53,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //上标 SuperscriptSpan spanstr.setSpan(new SuperscriptSpan(),53,56,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //文本外貌 TextAppearanceSpan family值:monospace serif sans-serif TextAppearanceSpan textSpan=new TextAppearanceSpan("sans-serif",Typeface.BOLD_ITALIC, getResources().getDimensionPixelSize(R.dimen.textSize),getResources().getColorStateList(R.color.colorAccent) ,getResources().getColorStateList(R.color.colorAccent)); spanstr.setSpan(textSpan,56,59,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //文本字体 TypefaceSpan family值:monospace serif sans-serif TypefaceSpan typefaceSpan=new TypefaceSpan("monospace"); spanstr.setSpan(typefaceSpan,59,63,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//感觉没什么变化 //文本超链接 URLSpan spanstr.setSpan(new URLSpan("http://music.163.com/#/song/464192018?userid=1337800695"),63,67, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //文本对齐方式 AlignmentSpan.Standard AlignmentSpan.Standard standard=new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); spanstr.setSpan(standard,0,67,Spannable.SPAN_INCLUSIVE_INCLUSIVE);3、使用textView展示
tv_span.setText(spanstr);
完美结束!