一、简介
SpannableString其实和String一样,都是一种字符串类型,同样TextView也可以直接设置SpannableString作为显示文本,不同的是SpannableString可以通过使用其方法setSpan方法实现字符串各种形式风格的显示,重要的是可以指定设置的区间,也就是为字符串指定下标区间内的子字符串设置格式。
二、基本使用
1. setSpan(Object what,int start,int end,int flags)
SpannableString通过这个方法设置各种文本样式,四个参数分别代表:
- what: 给文本设置的样式,如ForegroundColorSpan、RelativeSizeSpan
- start: 文本中需要设置样式的子文本的起始位置
- end: 文本中需要设置样式的子文本的结束位置
- flags: 计算起始位置和结束位置的模式,共有4种
flags 有以下几种:
- Spanned.SPAN_INCLUSIVE_EXCLUSIVE 包括起始下标,不包括结束下标
- Spanned.SPAN_INCLUSIVE_INCLUSIVE从 包括前,包括后
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 不包括前,不包括后
- Spanned.SPAN_EXCLUSIVE_INCLUSIVE 不包括前,包括后
经过测试,这些flag并没有什么卵用,不管设置哪个flag,始终是包前不包后
2. 常用的样式
效果图:
1. ForegroundColorSpan——前景色
// ForegroundColorSpan
TextView tvForegroundColorSpan = (TextView) findViewById(R.id.tv_foregroundColorSpan);
SpannableString sb1 = new SpannableString(tvForegroundColorSpan.getText());
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#3187D2"));
sb1.setSpan(foregroundColorSpan,0,10, SPAN_INCLUSIVE_EXCLUSIVE); // 始终包前,不包后
tvForegroundColorSpan.setText(sb1);
2. BackgroundColorSpan——背景色
// BackgroundColorSpan
TextView tvBackgroundColorSpan = (TextView) findViewById(R.id.tv_backgroundColorSpan);
SpannableString sb2 = new SpannableString(tvBackgroundColorSpan.getText());
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#F5A721"));
// 结束位置下标可以设置为字符串的长度,因为不管flag如何设置都不会包含最后一个字符,不会下标越界;开始位置下标不能小于0
sb2.setSpan(backgroundColorSpan,0,sb2.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
tvBackgroundColorSpan.setText(sb2);
3. RelativeSizeSpan——文字相对大小
// RelativeSizeSpan
TextView tvRelativeSizeSpan = (TextView) findViewById(R.id.tv_relativeSizeSpan);
SpannableString sb3 = new SpannableString(tvRelativeSizeSpan.getText());
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);
sb3.setSpan(relativeSizeSpan,0,12, SPAN_EXCLUSIVE_EXCLUSIVE);
tvRelativeSizeSpan.setText(sb3);
4. AbsoluteSizeSpan——文字大小
// AbsoluteSizeSpan
TextView tvAbsoluteSizeSpan = (TextView) findViewById(R.id.tv_absoluteSizeSpan);
SpannableString sb4 = new SpannableString(tvAbsoluteSizeSpan.getText());
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(22,true);
sb4.setSpan(absoluteSizeSpan,0,12, SPAN_INCLUSIVE_INCLUSIVE);
tvAbsoluteSizeSpan.setText(sb4);
5. StrikethroughSpan——删除线
// StrikethroughSpan
TextView tvStrikethroughSpan = (TextView) findViewById(R.id.tv_strikethroughSpan);
SpannableString sb5 = new SpannableString(tvStrikethroughSpan.getText());
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
sb5.setSpan(strikethroughSpan,0,sb5.length(), SPAN_INCLUSIVE_EXCLUSIVE);
tvStrikethroughSpan.setText(sb5);
6. UnderlineSpan——下划线
// UnderlineSpan
TextView tvUnderlineSpan = (TextView) findViewById(R.id.tv_underlineSpan);
SpannableString sb6 = new SpannableString(tvUnderlineSpan.getText());
UnderlineSpan underlineSpan = new UnderlineSpan();
sb6.setSpan(underlineSpan,0,sb6.length(), SPAN_INCLUSIVE_INCLUSIVE);
tvUnderlineSpan.setText(sb6);
7. SuperscriptSpan——上标
// SuperscriptSpan
TextView tvSuperscriptSpan = (TextView) findViewById(R.id.tv_superscriptSpan);
SpannableString sb7 = new SpannableString(tvSuperscriptSpan.getText());
SuperscriptSpan superscriptSpan = new SuperscriptSpan();
sb7.setSpan(superscriptSpan,11,sb7.length(), SPAN_EXCLUSIVE_INCLUSIVE);
tvSuperscriptSpan.setText(sb7);
8. SubscriptSpan——下标
// SubscriptSpan
TextView tvSubscriptSpan = (TextView) findViewById(R.id.tv_subscriptSpan);
SpannableString sb8 = new SpannableString(tvSubscriptSpan.getText());
SubscriptSpan subscriptSpan = new SubscriptSpan();
sb8.setSpan(subscriptSpan,9,sb8.length(), SPAN_INCLUSIVE_INCLUSIVE);
tvSubscriptSpan.setText(sb8);
9. StyleSpan——字体风格
// StyleSpan
TextView tvStyleSpan = (TextView) findViewById(R.id.tv_styleSpan);
SpannableString sb9 = new SpannableString(tvStyleSpan.getText());
StyleSpan styleSpan1 = new StyleSpan(Typeface.BOLD);
StyleSpan styleSpan2 = new StyleSpan(Typeface.ITALIC);
sb9.setSpan(styleSpan1,0,5, SPAN_EXCLUSIVE_EXCLUSIVE);
sb9.setSpan(styleSpan2,5,sb9.length(), SPAN_INCLUSIVE_INCLUSIVE);
tvStyleSpan.setText(sb9);
10. ImageSpan——图文混排
// ImageSpan
TextView tvImageSpan = (TextView) findViewById(R.id.tv_imageSpan);
Drawable drawable = ContextCompat.getDrawable(this,R.drawable.smile);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
SpannableString sb10 = new SpannableString(tvImageSpan.getText());
ImageSpan imageSpan = new ImageSpan(drawable);
sb10.setSpan(imageSpan,5,6, SPAN_EXCLUSIVE_EXCLUSIVE);
tvImageSpan.setText(sb10);
11. URLSpan——超链接文本
// UrlSpan
TextView tvUrlSpan = (TextView) findViewById(R.id.tv_urlSpan);
SpannableString sb11 = new SpannableString(tvUrlSpan.getText());
URLSpan urlSpan = new URLSpan("https://www.jianshu.com/u/3154fc47383d");
sb11.setSpan(urlSpan,0,sb11.length(), SPAN_INCLUSIVE_INCLUSIVE);
tvUrlSpan.setMovementMethod(LinkMovementMethod.getInstance());
tvUrlSpan.setText(sb11);
3. SpannableStringBuilder
和StringBuilder类似,可以拼接各个SpannableString,实现各种样式的拼接,同样也是append()方法
// SpannableStringBuilder
TextView tvSsb = (TextView) findViewById(R.id.tv_ssb);
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(sb1 );
ssb.append(sb2 );
ssb.append(sb3 );
tvSsb.setText(ssb);
自己练习了一遍,并记录一下。以后不至于使用这几个简单的API还要去查找了……
Github示例:SpannableStringDemo
个人总结,水平有限,如果有错误,希望大家能给留言指正!如果对您有所帮助,可以帮忙点个赞!如果转载,希望可以留言告知并在显著位置保留草帽团长的署名和标明文章出处!最后,非常感谢您的阅读!