在 Android 的 textview [ Html.fromhtml() ] 中使用多种文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10392163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:32:40  来源:igfitidea点击:

Using multiple text colors in Android's textview [ Html.fromhtml() ]

androidhtmlandroid-layoutformattingtextview

提问by user1029167

In one textview I'm trying to separate text stylings on different words.

在一个文本视图中,我试图将不同单词的文本样式分开。

So far I have:

到目前为止,我有:

textview.setText(Html.fromHtml("<i><small><font color=\"c5c5c5\">" + "Competitor ID: " + "</font></small></i>" + "<font color=\"47a842\">" + compID + "</font>"));

Basically, I'm saying I want "Competitor ID:"to be italic, small, and grey.

基本上,我是说我想要"Competitor ID:"斜体、小号和灰色。

And I want that string compIDto be normal sized, not italic, and green.

我希望该字符串compID为正常大小,而不是斜体和绿色。

The italic and small portions of the code work perfectly, but for some reason, the entire text view has grey text, rather than just "Competitor ID:".

代码的斜体和小部分工作得很好,但由于某种原因,整个文本视图都有灰色文本,而不仅仅是"Competitor ID:".

Any ideas?

有任何想法吗?

回答by Law Gimenez

textview.setText(Html.fromHtml("<i><small><font color=\"#c5c5c5\">" + "Competitor ID: " + "</font></small></i>" + "<font color=\"#47a842\">" + compID + "</font>"));

Try the above.

试试上面的。

回答by lastshadowrider

Try using one of these grey hex code values instead.

尝试改用这些灰色十六进制代码值之一。

gray1 #030303   
gray2 #050505  
gray3 #080808     
gray4 #0A0A0A   
gray5 #0D0D0D 

. Also try to control the text color inside the xml textview properties using the above codes. Hope this helps

. 还尝试使用上述代码控制 xml textview 属性内的文本颜色。希望这可以帮助