Span 对于android初学者来讲显得有点陌生,但却拥有很多强大的功能,本文以TextView为例,文中错误之处,还望高手指出。先上效果图:

            Android Span的各种使用方法,简单、易懂、全面、详细_第1张图片

一:首先说一下 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文本进行设置
//插入图片  IconMarginSpanBitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);spanstr.setSpan(new IconMarginSpan(bitmap,10),0,1,Spannable.SPAN_INCLUSIVE_INCLUSIVE);//图片插入文本 DrawableMarginSpanspanstr.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);//浮雕效果 EmbossMaskFilterEmbossMaskFilter filter=new EmbossMaskFilter(new float[]{20,20,20},0.5f,1,10);spanstr.setSpan(new MaskFilterSpan(filter),24,27,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//删除线(中划线)StrikethroughSpanspanstr.setSpan(new StrikethroughSpan(),27,29,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//下划线 UnderlineSpanspanstr.setSpan(new UnderlineSpan(),29,32,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//竖线   QuoteSpanspanstr.setSpan(new QuoteSpan(Color.RED),32,33,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//绝对大小 AbsoluteSizeSpanspanstr.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);//图片 ImageSpanImageSpan imageSpan=new ImageSpan(getResources().getDrawable(R.drawable.icon));spanstr.setSpan(imageSpan,38,40,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//字体相对大小 RelativeSizeSpanspanstr.setSpan(new RelativeSizeSpan(2f),40,43,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//基于X轴缩放  ScaleXSpanspanstr.setSpan(new ScaleXSpan(2f),43,45,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//字体样式,粗体 BOLD、斜体 ITALIC等 StyleSpanspanstr.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),45,50,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//下标 SubscriptSpanspanstr.setSpan(new SubscriptSpan(),50,53,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//上标 SuperscriptSpanspanstr.setSpan(new SuperscriptSpan(),53,56,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//文本外貌 TextAppearanceSpan       family值:monospace    serif   sans-serifTextAppearanceSpan 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-serifTypefaceSpan typefaceSpan=new TypefaceSpan("monospace");spanstr.setSpan(typefaceSpan,59,63,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//感觉没什么变化//文本超链接  URLSpanspanstr.setSpan(new URLSpan("http://music.163.com/#/song/464192018?userid=1337800695"),63,67,        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//文本对齐方式   AlignmentSpan.StandardAlignmentSpan.Standard standard=new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);spanstr.setSpan(standard,0,67,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
3、使用textView展示
tv_span.setText(spanstr);

完美结束!


更多相关文章

  1. Android多屏幕适配之字体大小、行间距和字间距
  2. [Android] Eclipse Android中设置模拟器屏幕大小几种方法
  3. Android 设置TextView字体Color Selector的正确方式
  4. 【Android Demo】Android中取得手机屏幕大小
  5. 【Android Demo】让Android支持自定义的ttf字体
  6. Android字体
  7. Android应用程序支持大小不同的屏幕

随机推荐

  1. 堆栈/帧指针作为外部变量
  2. Linux最常用的基础命令 上篇
  3. Linux回调函数的应用---已经验证
  4. Ubuntu 一键安装下LAMP安装配置
  5. 解决nfs链接开发板出现:nfs:server is not
  6. fedora(linux)创建系统服务 程序开机自启
  7. Android(安卓)Jetpack 使用入门
  8. Nagios 监控 Linux 服务器
  9. 创建链表的小例子
  10. 让 Linux 启动时加载自己的驱动模块 .ko