更改一个字的文字颜色在一个TextView [英] Change text color of one word in a TextView
本文介绍了更改一个字的文字颜色在一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要寻找一种方式来从活动中更改一个字的文本的颜色在一个TextView。
I am looking for a way to change the color of text of a single word in a TextView from within an activity.
例如这一点:
String first = "This word is ";
String next = "red"
TextView t = (TextView) findViewById(R.id.textbox);
t.setText(first + next);
我将如何修改接下来
文字为红色的颜色?
How would I change the color of the next
text to red?
推荐答案
我知道最简单的方法是只使用HTML。
Easiest way I know is to just use html.
String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));
但是,这需要您重建TextView的时候(如果?)你想改变颜色,这可能会造成麻烦。
But this will require you to rebuild the TextView when (if?) you want to change the color, which could cause a hassle.
这篇关于更改一个字的文字颜色在一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文