我在谷歌文档中阅读了 flags 参数,但我找不到它到底在做什么。例如在下面的代码中:
SpannableStringBuilder builder1 = new SpannableStringBuilder();
builder1.append("hi");
builder1.append("this is test");
builder1.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
myTextView.setText(builder1);
如果您更改第 4 个参数并设置如下变量,则没有区别:Spanned.SPAN_INCLUSIVE_INCLUSIVE,Spanned.SPAN_INCLUSIVE_EXCLUSIVE,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE。
结果没有区别。
但是当运行这段代码时:
SpannableStringBuilder builder1 = new SpannableStringBuilder();
builder1.append("hi");
builder1.append("this is test");
builder1.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
SpannableStringBuilder builder2 = new SpannableStringBuilder();
builder1.append("second test this is going to be different");
builder1.setSpan(new BackgroundColorSpan(Color.BLUE), 10, 15, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
SpannableStringBuilder all = new SpannableStringBuilder();
all.append(builder2);
all.insert(0, "\n");
all.insert(0, builder1);
根据传递给 builder1.setSpan 的第四个参数,您会得到不同的结果
有人可以解释为什么会这样吗?