Android的多色彩于一体的TextView多色、Android、TextView

2023-09-03 21:44:32 作者:爷蹲街。戏小妞

可能重复:   Is有可能有一个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>.";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
 
精彩推荐