太字にしてテキストの色を変更したい文の特定の単語のリストがあります。なぜか文字色の変更は効いているのですが、太字が反映されていないようです。さらに奇妙なのは、書体を斜体に変更すると反映されることです。私の質問は、テキストが太字にならない原因は何ですか?
// 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);
これにより、色が変化した正しい単語が得られますが、太字はありません。Italic に更新すると、すべて機能します。太字のみが機能しません。なぜ何か考えはありますか?
さらに詳しく調べるために、これが私の 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