1概述
SpannableStringBuilder 和SpannableString类似String,都可以存储文本,SpannableString类似String生成的String不可变,SpannableStringBuilder 类似StringBuilder生成的String可以append添加,不同的是还可以给文本局部添加额外样式描述,类似下划线,颜色,字体大小,click事件等等。
public class SpannableStringBuilder implements CharSequence, GetChars, Spannable, Editable,
Appendable, GraphicsOperations {
public class SpannableString
extends SpannableStringInternal
implements CharSequence, GetChars, Spannable
实现了CharSequence接口,所以SpannableStringBuilder ,SpannableString可以当做String使用。
2 简单使用
类似TextView等可以直接接收SpannableString,如果设置了样式就会展示特定样式,如果是一些不支持SpannableString的就按照String进行展示。
SpannableStringBuilder 和SpannableString设置样式是通过设置特定样式span实现, setSpan(fc,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);其中fc是特定样式的span,0,1是起始和结束位置,最后一个是个flag标识,有四个取值:
– Spannable.SPAN_EXCLUSIVE_EXCLUSIVE:前后都不包括,即在指定范围的前-面和后面插入新字符都不会应用新样式
– Spannable.SPAN_EXCLUSIVE_INCLUSIVE :前面不包括,后面包括。即仅在范围字符的后面插入新字符时会应用新样式
– Spannable.SPAN_INCLUSIVE_EXCLUSIVE :前面包括,后面不包括。
– Spannable.SPAN_INCLUSIVE_INCLUSIVE :前后都包括。
SpannableStringBuilder ssb = new SpannableStringBuilder("abcdefghijklmnopqrstuvwxyz");
ForegroundColorSpan fc = new ForegroundColorSpan(Color.RED);
AbsoluteSizeSpan abs = new AbsoluteSizeSpan(55, true);
ssb.setSpan(fc,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
mTv.setText(ssb);//mTv是个TextView
使用比较简单下面列举不同类型的Span:
字体颜色设置(ForegroundColorSpan)
字体背景颜色(BackgroundColorSpan)
字体大小(AbsoluteSizeSpan)
字体样式包括粗体、斜体等(StyleSpan)
StyleSpan span = new StyleSpan(Typeface.BOLD);
删除线(StrikethroughSpan)
下划线(UnderlineSpan)
3特殊Span设置
Span显示图片:
TextView中显示图片,一般是drawable:
drawable需要设置setBounds,否则图片无法显示。
SpannableStringBuilder ssb = new SpannableStringBuilder("abcdefghijklmnopqrstuvwxyz");
ForegroundColorSpan fc = new ForegroundColorSpan(Color.RED);
AbsoluteSizeSpan abs = new AbsoluteSizeSpan(55, true);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
ssb.setSpan(fc,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(imageSpan,5,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTv.setText(ssb);
文本设置单击事件:
SpannableStringBuilder ssb = new SpannableStringBuilder("abcdefghijklmnopqrstuvwxyz");
ForegroundColorSpan fc = new ForegroundColorSpan(Color.RED);
AbsoluteSizeSpan abs = new AbsoluteSizeSpan(55, true);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"spanClick",Toast.LENGTH_LONG).show();
}
};
mTv.setMovementMethod(LinkMovementMethod.getInstance());
ssb.setSpan(clickableSpan,10,15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(fc,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(imageSpan,5,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTv.setText(ssb);
需要添加 mTv.setMovementMethod(LinkMovementMethod.getInstance());,否则添加的click事件无效。
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"spanClick",Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
ds.setColor(Color.BLUE);
}
};
重写updateDrawState可以设置不同的样式,去掉下划线,设置颜色,默认带有下划线。
设置多个Span展示多个效果:
SpannableStringBuilder ssb = new SpannableStringBuilder("abcdefghijklmnopqrstuvwxyz");
ForegroundColorSpan fc = new ForegroundColorSpan(Color.RED);
ForegroundColorSpan fcc = new ForegroundColorSpan(Color.RED);
AbsoluteSizeSpan abs = new AbsoluteSizeSpan(55, true);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this,"spanClick",Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
ds.setColor(Color.BLUE);
}
};
mTv.setMovementMethod(LinkMovementMethod.getInstance());
ssb.setSpan(clickableSpan,10,15,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(fc,0,1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(fcc,20,22, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssb.setSpan(imageSpan,5,9,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTv.setText(ssb);