多个BackgroundColorSpan错误地显示Android API 24 25

时间:2017-07-14 19:25:54

标签: android textview spannable

我使用BackgroundColorSpan显示TextView的多个高光。它在Android API 23及更低版本上正确显示,但在API 24和25中存在问题。

final static String article1 = "Last year’s Google Pixel and Pixel XL were without question " +
        "two of the hottest new smartphones of 2016. Google surprised fans by ditching its " +
        "Nexus lineup of devices and moving from affordable upper mid-range devices to " +
        "high-end flagship phones. Sure they were more expensive than Google phones had " +
        "been in the past, but they were also a much better platform for showcasing the best " +
        "of what Android has to offer, featuring high-end specs and a sleek metal and " +
        "glass design.";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SpannableStringBuilder sb = new SpannableStringBuilder(article1);

    BackgroundColorSpan span;
    for (int i = 0; i < 3; i++) {
        span = new BackgroundColorSpan(Color.YELLOW);
        sb.setSpan(span, 23, 214, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    span = new BackgroundColorSpan(Color.RED);
    sb.setSpan(span, 67, 83, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    span = new BackgroundColorSpan(Color.RED);
    sb.setSpan(span, 216, 236, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView tv = (TextView) findViewById(R.id.body);
    tv.setText(sb);
}

}

如果是循环3,则只显示一个红色高亮显示。 如果是循环2,则会显示两个红色高光。

1 个答案:

答案 0 :(得分:0)