同一字符串中的多个跨度

Tof*_*sio 0 android textview spannable

我在同一个字符串中设置 StyleSpan 和 ForegroundColorSpan 时遇到问题。这是我的代码:

SpannableStringBuilder text_1_2 = new SpannableStringBuilder(getString(R.string.why_text_1_2));

StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
ForegroundColorSpan fcs = new ForegroundColorSpan(getResources().getColor(R.color.custom_blue));

text_1_2.setSpan(fcs, 0 , text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
text_1_2.setSpan(bss, 0, text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

结果字符串为蓝色,但不是粗体。

谢谢。

Fah*_*him 5

你能试试这个吗

text_1_2.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0 , text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
text_1_2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.custom_blue)), 0, text_1_2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
Run Code Online (Sandbox Code Playgroud)

但我的建议是,如果您不想为字符串的一部分设置样式,请使用

 android:textColor="@color/color_name"
 android:textStyle="bold"
Run Code Online (Sandbox Code Playgroud)