以下代码适用于我在2.3和6.0.1之间尝试的所有内容,但无法使用7.0 / Emulator和7.1.1 / Nexus 6P
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
for (int i=0;i<5;++i) {
int p = spannableStringBuilder.length();
spannableStringBuilder.append("aAAAAa\n");
spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), p+1, p+5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
boolean pass = true;
StyleSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), StyleSpan.class);
int p = 0;
for (StyleSpan span : spans) {
int start = spannableStringBuilder.getSpanStart(span);
if (start<p) {
pass = false;
break;
}
p = start;
}
答案 0 :(得分:3)
当前的解决方法我正在使用
Arrays.sort(spans, new Comparator<ClickableSpan>() {
@Override
public int compare(ClickableSpan o1, ClickableSpan o2) {
return s.getSpanStart(o1)-s.getSpanStart(o2);
}
});