SpannableString 和 TextAppearanceSpan,仅应用颜色

Rém*_*émy 6 android textview

我想在一个文本视图中有 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);
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

但是,只有 android:textColor 是适用的。

你能用这种方法帮助有其他风格吗?

谢谢

Rém*_*émy 6

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

爪哇:

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);
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)