通俗来讲,就是富文本的Android实现方式,跟iOS的NSMutableAttributedString一样的作用
使用方法
SpannableString spannableString = new SpannableString("我是一段需要被修饰的字符串");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
spannableString.setSpan(backgroundColorSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.setText(spannableString);
setSpan
这个是核心方法,有四个参数
参数1:具体要修改的样式,颜色、大小等等,上图代码就是修改颜色
参数2:修改的起始下标
参数3:修改的结束下标
参数4:就是是否包含开始坐标或者结束坐标,排列组合下面四种类型
`SPAN_INCLUSIVE_EXCLUSIVE`:包括开始下标,但不包括结束下标
`SPAN_EXCLUSIVE_INCLUSIVE`:不包括开始下标,但包括结束下标
`SPAN_INCLUSIVE_INCLUSIVE`:既包括开始下标,又包括结束下标
`SPAN_EXCLUSIVE_EXCLUSIVE`:不包括开始下标,也不包括结束下标
记录几个常用的 span
(不知道是不是这么叫,反正用于setSpan
)
-
ForegroundColorSpan
修改颜色
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
-
BackgroudColorSpan
修改背景色
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
-
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);
-
URLSpan
文字超链接
URLSpan urlSpan = new URLSpan("https://www.baidu.com/s?ie=UTF-8&wd=字符串");
spannableString.setSpan(urlSpan, 10, 13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
mTextView.setText(spannableString);
-
RelativeSizeSpan
文字相对大小,相比其他文字的倍数
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.5f);
-
AbsoluteSizeSpan
设置文字大小,第一个参数是大小,第二个参数true代表dp,false代表px
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(40, true);
-
StyleSpan
设置字体样式,斜体粗体等
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);
-
TypefaceSpan
设置文字字体类型
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);
-
TextAppearanceSpan
通过系统的style资源设置字体样式
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, android.R.style.TextAppearance_Material);
spannableString.setSpan(textAppearanceSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
-
UnderlineSpan
设置文字下划线
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
-
StrikethroughSpan
设置文字删除线
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
-
SuperscriptSpan
设置文字为上标
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);
-
SubscriptSpan
设置文字为下标
SubscriptSpan subscriptSpan = new SubscriptSpan();
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.8f);
spannableString.setSpan(relativeSizeSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(subscriptSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
-
ImageSpan
文字上拼接图片
SpannableString spannableString = new
ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_eason);
spannableString.setSpan(imageSpan, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
网友评论