Android使用Span打造丰富多彩的文本详解
1.引言
在开发过程中经常需要使用文本,有时候需要对一段文字中的部分文字进行特殊的处理,如改变其中部分文字的大小、颜色、加下划线等,这个时候使用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支持单独使用和组合使用,使用它能够对文本进行各种灵活的操作,去实现个性化的需求。
到此这篇关于android使用span打造丰富多彩的文本详解的文章就介绍到这了,更多相关android span文本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 读写分离 主从复制
下一篇: MySQL打印死锁日志的方法步骤