不同的颜色在值的列表是在一个TextView [英] different colors in a list of values that are in a TextView
问题描述
我的问题是我有一个的TextView
使用不同的值,例如:
-1.7
+5,8
-3,3
0,0
+7,7
My question is I have a TextView
with different values, for example:
-1.7
+5,8
-3,3
0,0
+7,7
随着code,我也只取前负值,给我留下的所有相同的颜色。
With the code that I did just take the first negative value and leaves me all the same color.
这是我的code:
活动:
List<ParsedExampleDataSet5101> parsedExampleDataSetList =
myExampleHandler.getParsedData();
for(ParsedExampleDataSet5101 parsedExampleDataSet : parsedExampleDataSetList){
tv.append(parsedExampleDataSet.toString());
tv1.append(parsedExampleDataSet.toString1());
tv2.append(parsedExampleDataSet.toString2());
tv3.append(parsedExampleDataSet.toString3());
if (parsedExampleDataSet.toString2().contains("-")){
tv2.setTextColor(Color.parseColor("#FF0000"));
}else{
tv2.setTextColor(Color.parseColor("#008000"));
}
}
ParsedExampleDataSet5101
是其他类,我在这个类的getter和setter和的toString()
ParsedExampleDataSet5101
is an other class, I have in this class the getters and setters and toString()
推荐答案
Ridcully基本上是正确的,但他的回答是不完整的 - 而且也不支持span标签
Ridcully is basically right, though his answer is not complete - and the span tag is also not supported.
您可以在Textviews使用HTML,但你必须创建一个跨区(CharSequence的子类)包含格式信息。
You can use HTML in Textviews, but you have to create a Spanned (subclass of CharSequence) that contains the formatting information.
您创建一个使用Html.fromHtml跨区()。有关HTML细节TextViews格式,请参阅我的文章
与Html.fromHtml)设置文本格式(
You create a Spanned by using Html.fromHtml(). For details on HTML formatting in TextViews, see my post Formatting text with Html.fromHtml()
所以你可以做这样的事情:
So you could do something like this:
if (parsedExampleDataSet.toString2().startsWith("-")){
tv2.append(Html.fromHtml("<font color=\"#ff0000\">" + parsedExampleDataSet.toString2() + "</font>"));
}else{
tv2.append(parsedExampleDataSet.toString2());
}
这篇关于不同的颜色在值的列表是在一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!