使 SpannableStringBuilder 的多个部分变为粗体

Making multiple parts of a SpannableStringBuilder Bold

我正在尝试在 TextView 中获取一个字符串,它应该如下所示:

BOLD not bold BOLD not bold

我试过这个代码:

val builder = SpannableStringBuilder()
val boldSpan = StyleSpan(Typeface.BOLD)
val spanFlag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

builder.append("BOLD ", boldSpan, spanFlag)
builder.append("not bold ")
builder.append("BOLD ", boldSpan, spanFlag)
builder.append("not bold ")

return builder

问题是它只是将最后一部分加粗。结果如下所示:

BOLD not bold BOLD not bold

有没有办法使用 SpannableStringBuilder 将多个部分加粗?

使用两个不同的跨度:

val builder = SpannableStringBuilder()
val spanFlag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

builder.append("BOLD ", StyleSpan(Typeface.BOLD), spanFlag)
builder.append("not bold ")
builder.append("BOLD ", StyleSpan(Typeface.BOLD), spanFlag)
builder.append("not bold ")

return builder