同一个SpannableStringBuilder可以应用多个setSpan吗?

Can multiple setSpan be applied to the same SpannableStringBuilder?

我有一个句子中的特定单词列表,我想将其加粗并更改文本颜色。出于某种原因,文本颜色更改有效,但似乎未应用粗体。更奇怪的是,将字体更改为 Italic 反映了。我的问题是,是什么导致我的文本不加粗?

// lets create a list of words
ArrayList<String> alWords = new ArrayList<>();
alWords.add("Mary");
alWords.add("lamb");

// had earlier StringBuilder to form 'Mary had a little lamb.' for example
SpannableStringBuilder sentence = new SpannableStringBuilder(sb.toString());

// iterate through list of words and bold & color change text
for (int i = 0; i < alWords.size(); i++) {
   Pattern word = Pattern.compile(alWords.get(i));
   Matcher match = word.matcher(sentence);

   int startPos = 0;
   int endPos = 0;

   while (match.find()) {
      startPos = match.start();
      endPos = match.end();
   }
   sentence.setSpan(new StyleSpan(Typeface.BOLD), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   sentence.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.green)), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} 

tvText.setText(sentence, TextView.BufferType.SPANNABLE);

这会导致正确的单词发生颜色变化,但不会加粗。如果您更新为斜体,它也可以正常工作。只有粗体文本不起作用。有什么想法吗?

为了进一步检查,这是我的 dumpSpans 日志:

span= Mary: 1c181a6 android.text.style.StyleSpan (0-4) fl=#33 
span= Mary: 148f7e7 android.text.style.ForegroundColorSpan (0-4) fl=#33 
span= lamb: b441f94 android.text.style.StyleSpan (18-22) fl=#33 
span= lamb: e18ba3d android.text.style.ForegroundColorSpan (18-22) fl=#33

我使用 SpannableString 而不是 SpannableStringBuilder 并且效果很好。
试试这个代码:

SpannableString sentence = new SpannableString(sb.toString());

然后是

tvText.setText(sentence);