android - 我正在尝试在 Marshamallow 上使用 spannable。它给了我不正确的输出

标签 android textview spannablestring spannable

        SpannableString text = new SpannableString("RAHUL");
        text.setSpan(new ForegroundColorSpan(white), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(blue), 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        headerHolder.mTextViewLabel.setText(text, TextView.BufferType.SPANNABLE);

我只得到“HL”作为输出。

最佳答案

通过输出检查这一点

  SpannableString text = new SpannableString("RAHUL");
        text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 3, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv.setText(text);

棉花糖输出

enter image description here

希望对你有帮助

关于android - 我正在尝试在 Marshamallow 上使用 spannable。它给了我不正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37916744/

相关文章:

java - 以编程方式在 LinearLayout 中并排设置两个 TextView

android - 在 TextView 中显示所有 Unicode 字符

android - java.lang.StackOverflowError : stack size 8MB while Enabling/Disabling TextView#setTextIsSelectable in ListView Adapter 错误

java - 如何为字符串的数字和字符设置不同的颜色和大小

android - 圆形 imageview 边框未显示

java - 避免通过不受信任的证书连接

java - 在android中显示时区

android - 如何将应用程序从生产转移到 beta android

android - 为 RecyclerView 缓存 Spannable 字符串

java - 如何在 addTextChangedListener 中使用可扩展字符串来更改 editText 子字符串的颜色?