记SpannableString设多少span时注意事项

public void setSpan(Object what, int start, int end, int flags) {    }

 这个方法里的第一个参数,也就是一些span的对象,不能重复使用。如下

ForegroundColorSpan fcs = new ForegroundColorSpan(color);
ss.setSpan(fcs, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
ss.setSpan(fcs, 4, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

 效果只会运用到4~6这段,而不会是运用到两处。改成下面这样就可以了

ss.setSpan(new ForegroundColorSpan(color), 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
ss.setSpan(new ForegroundColorSpan(color), 4, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

 注:每次传入的第一个参数,都需要是一个新的对象。

 
posted @ 2017-01-06 10:47  WY彪  阅读(1750)  评论(1编辑  收藏  举报