写点什么

Android SpannableString 详细解析

发布于: 2021 年 11 月 06 日

可以看出所有 Span 都继承于 CharacterStyle 这个抽象类,另外 MetricAffectingSpan、ReplacementSpan 和 ClickableSpan 都是抽象类,下面展示一些常用的 Span


ForegroundColorSpan 前景色




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.GREEN);


spannableString.setSpan(foregroundColorSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


ForegroundColorSpan:前景色,也就是对文字上色,颜色设置为 GREEN,start 为 4,end 为 7,应该是“陈奕迅”三个字显示为绿色,看一下实际效果



BackgroudColorSpan 背景色




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);


spannableString.setSpan(backgroundColorSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


BackgroudColorSpan:与 ForegroundColorSpan 类似,对文字背景上色



ClickableSpan 点击事件




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


ClickableSpan clickableSpan = new ClickableSpan() {


@Override


public void onClick(View widget) {


Toast.makeText(MainActivity.this, "如果我是陈奕迅", Toast.LENGTH_SHORT).show();


}


@Override


public void updateDrawState(TextPaint ds) {


ds.setUnderlineText(false);


}


};


spannableString.setSpan(clickableSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setMovementMethod(LinkMovementMethod.getInstance());


mTextView.setText(spannableString);


ClickableSpan:是一个抽象类,实现可点击效果,可以重写 onClick 方法实现点击事件,这里点击“陈奕迅”三个字简单地弹 toast



URLSpan 超链接




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


URLSpan urlSpan = new URLSpan("https://www.baidu.com/s?ie=UTF-8&wd=陈奕迅");


spannableString.setSpan(urlSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setMovementMethod(LinkMovementMethod.getInstance());


mTextView.setText(spannableString);


URLSpan:实现超链接的效果,继承于 ClickableSpan,点击实现跳转到浏览器



MaskFilterSpan 模糊 浮雕




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


MaskFilterSpan embossMaskFilterSpan =


new MaskFilterSpan(new EmbossMaskFilter(new float[]{10, 10, 10}, 0.5f, 1, 1));


spannableString.setSpan(embossMaskFilterSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);


spannableString.setSpan(relativeSizeSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


MaskFilterSpan blurMaskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(10, Blur.NORMAL));


spannableString.setSpan(blurMaskFilterSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


MaskFilterSpan:构造方法接受 MaskFilter 作为参数,其中它有两个子类:EmbossMaskFilter 和 BlurMaskFilter


EmbossMaskFilter 实现浮雕效果


EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius)


  • direction:float 数组,定义长度为 3 的数组标量[x,y,z],来指定光源的方向

  • ambient:环境光亮度,0~1

  • specular:镜面反射系数

  • blurRadius:模糊半径,必须>0


BlurMaskFilter 实现模糊效果


BlurMaskFilter(float radius, Blur style)


  • radius:模糊半径

  • style:有四个参数可选

  • BlurMaskFilter.Blur.NORMAL:内外模糊

  • BlurMaskFilter.Blur.OUTER:外部模糊

  • BlurMaskFilter.Blur.INNER:内部模糊

  • BlurMaskFilter.Blur.SOLID:内部加粗,外部模糊



RelativeSizeSpan 字体相对大小




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);


spannableString.setSpan(relativeSizeSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


RelativeSizeSpan:设置字体的相对大小,这里设置为 TextView 大小的 1.5 倍,看图



AbsoluteSizeSpan 字体绝对大小




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(40, true);


spannableString.setSpan(absoluteSizeSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


AbsoluteSizeSpan:设置字体的相绝对大小,40 表示文字大小,true 表示单位为 dip,若为 false 则表示 px



ScaleXSpan 字体 x 轴缩放




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


Scal


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


eXSpan scaleXSpan= new ScaleXSpan(1.5f);


spannableString.setSpan(scaleXSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


ScaleXSpan:设置字体 x 轴缩放,1.5 表示 x 轴放大为 1.5 倍,效果如图



StyleSpan 样式




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);


StyleSpan italicSpan = new StyleSpan(Typeface.ITALIC);


StyleSpan boldItalicSpan = new StyleSpan(Typeface.BOLD_ITALIC);


spannableString.setSpan(boldSpan, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


spannableString.setSpan(italicSpan, 2, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


spannableString.setSpan(boldItalicSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


StyleSpan:设置文字样式,如斜体、粗体



TypefaceSpan 字体




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


TypefaceSpan monospace = new TypefaceSpan("monospace");


TypefaceSpan serif = new TypefaceSpan("serif");


TypefaceSpan sans_serif = new TypefaceSpan("sans-serif");


spannableString.setSpan(monospace, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


spannableString.setSpan(serif, 2, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


spannableString.setSpan(sans_serif, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


TypefaceSpan:设置文字字体类型,如 monospace、serif 和 sans-serif 等等



TextAppearanceSpan 文字外貌




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, android.R.style.TextAppearance_Material);


spannableString.setSpan(textAppearanceSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


TextAppearanceSpan:设置文字外貌,通过 style 资源设置,这里使用系统的 style 资源



UnderlineSpan 下划线




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


UnderlineSpan underlineSpan = new UnderlineSpan();


spannableString.setSpan(underlineSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


UnderlineSpan:设置文字下划线,强调突出文字时可以使用该 span



StrikethroughSpan 删除线




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


StrikethroughSpan strikethroughSpan = new StrikethroughSpan();


spannableString.setSpan(strikethroughSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


StrikethroughSpan:设置文字删除线



SuperscriptSpan 上标




SpannableString spannableString = new SpannableString("如果我是陈奕迅");


SuperscriptSpan superscriptSpan = new SuperscriptSpan();


RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.8f);


spannableString.setSpan(relativeSizeSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


spannableString.setSpan(superscriptSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);


mTextView.setText(spannableString);


SuperscriptSpan:设置文字为上标



SubscriptSpan 下标




评论

发布
暂无评论
Android SpannableString详细解析