更改TextView中一个单词的文本颜色 [英] Change text color of one word in a TextView

查看:256
本文介绍了更改TextView中一个单词的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来改变 TextView 中的单个文字的颜色。活动

I am looking for a way to change the color of a 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);

如何改变下一页的颜色 text to red?

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屋!

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