Android Span文本
i小灰 人气:01.引言
在开发过程中经常需要使用文本,有时候需要对一段文字中的部分文字进行特殊的处理,如改变其中部分文字的大小、颜色、加下划线等,这个时候使用Span就能方便地解决这些问题。本文将主要介绍SpannableStringBuilder和各种Span的使用。
2.SpannableStringBuilder的基本用法
新建一个SpannableStringBuilder对象的操作如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!");
SpannableStringBuilder的setSpan()方法如下:
//what:各种文本Span,如BackgroundColorSpan、ForegroundColorSpan等 //start:应用Span的文本的开始位置索引 //end:应用Span的文本的结束位置索引 //flags:标志 public void setSpan(Object what, int start, int end, int flags) { setSpan(true, what, start, end, flags, true/*enforceParagraph*/); }
3.使用Span给文本添加效果
3.1 AbsoluteSizeSpan
此Span用来改变文本的绝对大小,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new AbsoluteSizeSpan(60),3,9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.2 BackgroundColorSpan
此Span用来改变文本的背景颜色大小,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new BackgroundColorSpan(Color.GREEN),3,9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.3 ClickableSpan
此Span用来给文本添加点击效果,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new ClickableSpan() { @Override public void onClick(@NonNull View widget) { Toast.makeText(MainActivity.this,"ClickableSpan",Toast.LENGTH_SHORT).show(); } }, 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder); tv_content.setMovementMethod(LinkMovementMethod.getInstance()); tv_content.setHighlightColor(Color.TRANSPARENT);
3.4 DrawableMarginSpan
此Span用来给段落添加drawable和padding,这个padding指的是drawable和文本之间的距离,默认值是0,Span要从文本的起始位置设置,否则Span将不会渲染或者错误地渲染,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); Drawable drawable = AppCompatResources.getDrawable(MainActivity.this,R.drawable.ic_launcher); builder.setSpan(new DrawableMarginSpan(drawable,30), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.5 DynamicDrawableSpan
此Span使用drawable替换文本内容,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new DynamicDrawableSpan() { @Override public Drawable getDrawable() { Drawable drawable = AppCompatResources.getDrawable(MainActivity.this,R.drawable.ic_launcher); drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight()); return drawable; } }, 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.6 ForegroundColorSpan
此Span可以用来改变文本的颜色,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new ForegroundColorSpan(Color.GREEN), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.7 IconMarginSpan
此Span可以在文本开始的地方添加位图,而且可以在位图和文本之间设置padding,padding的默认值是0px,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); builder.setSpan(new IconMarginSpan(bitmap,30), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.8 ImageSpan
此Span可以使用Drawable替换文本,创建ImageSpan的构造方法有很多,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new ImageSpan(MainActivity.this,R.drawable.ic_launcher), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.9 MaskFilterSpan
此Span可以给文本设置MaskFilter,例如给文本设置模糊效果,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); MaskFilter maskFilter = new BlurMaskFilter(10f, BlurMaskFilter.Blur.NORMAL); builder.setSpan(new MaskFilterSpan(maskFilter), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.10 QuoteSpan
此Span可以在文本开始的地方添加一个垂直的线条,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new QuoteSpan(Color.GREEN), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.11 RelativeSizeSpan
此Span可以按一定的比例缩放文本的大小,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new RelativeSizeSpan(2.0f), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.12 ScaleXSpan
此Span以一定的系数在水平方向缩放文本的大小,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new ScaleXSpan(2.5f), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.13 StrikethroughSpan
此Span可以在文本上添加下划线,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new StrikethroughSpan(), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.14 StyleSpan
此Span可以设置文本的样式,可用的样式有Typeface.NORMAL、Typeface.BOLD、Typeface.ITALIC、Typeface.BOLD_ITALIC,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new StyleSpan(Typeface.BOLD), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.15 SubscriptSpan
此Span可以将文本的基线移动到更低的地方,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new SubscriptSpan(), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.16 SuperscriptSpan
此Span可以将文本的基线移动到更高的地方,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new SuperscriptSpan(), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
3.17 UnderlineSpan()
此Span可以在文本下面添加下划线,示例如下:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new UnderlineSpan(), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
4.多个Span组合使用
Span不但可以单独使用,还可以组合在一起使用,以下示例演示了如何同时加粗文字,改变文字的颜色和添加下滑线:
SpannableStringBuilder builder = new SpannableStringBuilder("Hello World!"); builder.setSpan(new UnderlineSpan(), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); builder.setSpan(new ForegroundColorSpan(Color.GREEN), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); builder.setSpan(new StyleSpan(Typeface.BOLD), 3, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv_content.setText(builder);
5.总结
Span的功能相当丰富,如改变文本颜色、大小、添加点击效果、加下划线等功能,本文介绍了经常用到的各种Span,Span支持单独使用和组合使用,使用它能够对文本进行各种灵活的操作,去实现个性化的需求。
加载全部内容