一个TextView中的Android多色 [英] Android multi color in one TextView

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

问题描述

可能重复:
TextView 中是否可以有多种样式?

我希望我的 TextView 将其文本的某些部分显示为红色,而将其他部分显示为黑色.它的内容(文本)是动态创建的,我不知道有多少单词会变成红色.

I want my TextView to show some part of its text in red and others in black. It's content (the text) is created dynamically and i don't know how many words will be red colored.

有没有办法像在 html-css 中那样做?

Is there any way to do this like in html-css?

推荐答案

你可以使用 Spannable 来实现你想要的.

You can use Spannable to achieve what you want.

String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   textView.setText(Html.fromHtml(text,  Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
} else {
   textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
}

这篇关于一个TextView中的Android多色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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