SpannableStringBuilder

SpannableStringBuilder

下面的代码适用于我在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;
}

最佳答案

我正在使用的当前解决方案

Arrays.sort(spans, new Comparator<ClickableSpan>() {
    @Override
    public int compare(ClickableSpan o1, ClickableSpan o2) {
        return s.getSpanStart(o1)-s.getSpanStart(o2);
    }
});

关于android - 在牛轧糖7.0/7.1上SpannableStringBuilder.getSpans()排序顺序错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41052172/

10-09 00:53