我正在尝试构建富文本,其中第一部分显示“正在查找:”并加粗,但其余部分未加粗。这是我的代码:
SpannableStringBuilder lookingForString = new SpannableStringBuilder("Looking for: ");
lookingForString.setSpan(new StyleSpan(Typeface.BOLD), 0, lookingForString.length(),
0);
int start = (lookingForString.length() - 1);
for (int i = 0; i < looking_for_names.length(); ++i) {
// No comma before the first and after the last 'Looking for' value
if (i > 0) {
lookingForString.append(", ");
lookingForString.setSpan(new StyleSpan(Typeface.NORMAL), start,
(lookingForString.length() - 1), 0);
start += 2;
}
String lfItem = looking_for_names.optString(i);
lookingForString.append(lfItem);
lookingForString.setSpan(new StyleSpan(Typeface.NORMAL), start,
(lookingForString.length() - 1), 0);
start += lfItem.length();
}
tvLookingFor.setText(lookingForString, BufferType.SPANNABLE);
然而,结果是整行都是粗体的。我尝试了很多变体,但我无法正确控制字体……无论我如何编码,它都倾向于保留第一个字体。
我怎样才能让“寻找:”只有粗体,但其余文本是常规的(非粗体)?