更改一个字的文字颜色在一个TextView一个字、颜色、文字、TextView

2023-09-12 00:32:42 作者:温柔的守候

我要寻找一种方式来从活动中更改一个字的文本的颜色在一个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.