如何更改我的TextView中的某一部分的颜色? [英] How do I change color of a certain part of my textview?
问题描述
我在一个TextView使用两部分,第一部分是另一个日期是姓名和电子邮件。
它们在相同的TextView两个引用。我想更改日期的颜色给它一个不同的视觉它的名字和电子邮件。是有可能做到这一点没有实际增加了姓名和电子邮件的全新的TextView?
这里是我的code迄今:
I am using 2 parts in a textview, 1st part is date another is name and email. They are both referenced in the same textview. I would like to change the color of the date to give it a different visual it from name and email. is it possible to do this without actually adding a whole new textview for name and email? Here's my code so far:
String nameandemail;
holder.mytext.setText(String.valueOf(dateFormat.format(new Date(msg.getDate())) + " " + nameandemail + ": "));
我如何让这样我可以设置日期的颜色 holder.mytext.setTextColor(Color.white)
并为nameandemail串像绿色的?
How do I make it such that I can set the color of date with
holder.mytext.setTextColor(Color.white)
and for the nameandemail string something like green?
谢谢!
推荐答案
您可以定义一个字符串的的strings.xml
文件
you could define a String in your strings.xml
file
<string name="test2"><font color=\'#FFFFFF\'>%1$s</font> -- <font color=\'#00FF00\'>%2$s</font></string>
,然后以编程
TextView tv = (TextView) findViewById(R.id.test);
tv.setText(Html.fromHtml(getString(R.string.test2, String.valueOf(dateFormat.format(new Date(msg.getDate())), nameandemail)));
这篇关于如何更改我的TextView中的某一部分的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!