dea*_*ish 5 android text styles spannablestring
我通过多个span样式设置了几个单词,当我将带有样式的数组传递给方法时,结果中只有最后一个单词具有该样式.它省略了其他的话.为什么?在我的代码和执行下面.先感谢您.
//execution in code
charSequence = SpannableUtils.format(
charSequence,
new ParcelableSpan[]{new StyleSpan(Typeface.BOLD)},//or more
new String[]{"Test1", "Test2"}
);
//method
public static CharSequence format(CharSequence charSequence, ParcelableSpan[] spans, String[] words) {
SpannableStringBuilder ssb = new SpannableStringBuilder(charSequence);
for (String word : words) {
Pattern pattern = Pattern.compile(Pattern.quote(word));
Matcher matcher = pattern.matcher(charSequence);
for (ParcelableSpan span : spans) {
while (matcher.find()) {
ssb.setSpan(span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return ssb;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我用'instance of'检查span的类型,然后我创建新的constructior它可以工作.为什么?
Flo*_*ern 10
您不能多次应用相同的span对象.所有先前设置的跨度都被丢弃,因此只有最后一个单词具有跨度.
如果要重复使用它,则必须CharacterStyle.wrap()在应用之前将其复制:
while (matcher.find()) {
ssb.setSpan(CharacterStyle.wrap(span), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
585 次 |
| 最近记录: |