var textView = findViewById<TextView>(R.id.main_TextView)
var s = "测试测试,再说一遍,这真的是测试文字!"
val spannable = Spannable.Factory.getInstance().newSpannable(s)//将字符串包装成可操作的文本格式
val backgroundColorSpan = BackgroundColorSpan(Color.parseColor("#FF0000"))//实例一个具体富文本格式工具类
spannable.setSpan(backgroundColorSpan, 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)//给0-9的字符设置成上面那种背景
tv!!.setText(spannable)
嗯,你没有看错,就是这么简单
接下来我的一般的富文本格式工具类罗列一下
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#FF0000"));//文字背景,也就是文字的颜色
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FF0000"));//文字前景
RelativeSizeSpan sizeSpanBig = new RelativeSizeSpan(1.4f);//放大或者缩小,取决于后面的浮点数大于或者小于一
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();//删除线,就是文字中间一条线
UnderlineSpan sizeSpan = new UnderlineSpan();//下划线
StyleSpan styleSpan_Bold = new StyleSpan(Typeface.BOLD);//粗体
StyleSpan styleSpan_Italic = new StyleSpan(Typeface.ITALIC);//斜体//这三句是构建一个图片,注意将图片设置进去的时候会覆盖那个第二个和第三个参数的所在的字
Drawable image = this.getResources().getDrawable(R.mipmap.star);
image.setBounds(0,0,60,60);
ImageSpan imageSpan = new ImageSpan(image);