SpannableString 和 TextAppearanceSpan,仅应用颜色

SpannableString and TextAppearanceSpan, apply only color

我想在一个文本视图中有 2 种文本样式,所以我尝试

Spannable text = new SpannableString(pseudo + " " + "some text after that");
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle), 0, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.TextStyle), pseudo.length() + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

holder.mText.setText(text);

在这里你可以找到我的风格

<style name="PseudoStyle">
  <item name="android:textAllCaps">true</item>
  <item name="android:textSize">14sp</item>
  <item name="android:textStyle">bold</item>
  <item name="android:textColor">@color/pink_light</item>
</style>
<style name="TextStyle">
  <item name="android:textColor">@color/white</item>
</style>

但总的来说,只有android:textColor适用。

你能帮忙用这个方法做成其他风格吗?

谢谢

你为什么不声明两种样式,然后像这样在SpannableString中的文本部分应用当前样式(这只是一个例子):

Spannable text = new SpannableString(pseudo + " " + "some text after that");
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle_1), 0, pseudo.length() - 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle_2), pseudo.length() - 10, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.mText.setText(text);

我找到了一个棘手的解决方案,正常添加 StyleSpan Bold 而不是按样式添加

java:

text.setSpan(new TextAppearanceSpan(mContext, R.style.PseudoStyle), 0, pseudo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, pseudo().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(mContext, R.style.TextStyle), pseudo.length() + 1, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

xml:

<style name="PseudoStyle">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/pink_light</item>
</style>

<style name="TextStyle">
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">@color/white</item>
</style>