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

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

问题描述

我正在寻找一种方法来从 Activity 中更改 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);

如何将 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屋!

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