添加字符串格式后颜色丢失 [英] Color is lost after adding string format

查看:63
本文介绍了添加字符串格式后颜色丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在textview中添加一些文本,其中某些文本为红色,某些文本为黑色,且具有特定格式

I need to add some text in a textview with some text with red color and some are black color with a specific formatting

Spannable wordtoSpan = new SpannableString(temp.substring(start, i));
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//failedToSolve.append(wordtoSpan); // was working when I used no formatting
failedToSolve.append(String.format("%7s", wordtoSpan)); // coloring is not working after I apply String.format on it

我知道我正在使用 String 类方法格式化 Spannable 对象.还有其他选择吗?如何一起进行格式化和着色?我使用了HTML标记,但没有效果.预先感谢.

I know I am formatting Spannable object with String class method. Is there any alternatives? How can I do the formatting and coloring together? I used HTML tag but no effects. Thanks in advance.

推荐答案

有其他选择吗?

首先调用 String.format(),然后从结果中创建 SpannableString ,并应用 ForegroundColorSpan .

Call String.format() first, then create the SpannableString from the result and apply the ForegroundColorSpan.

这篇关于添加字符串格式后颜色丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆