简体   繁体   English

如何将hexColor代码分配给Java类文件上的任何数据类型

[英]How to assign hexColor code to any datatype on java class file

I need to change a textColor on the UI scrolling. 我需要在UI滚动上更改textColor。 What i've done is I've declared the colors into Colors.xml files. 我所做的是将颜色声明为Colors.xml文件。

<color name="color1">#e0195c</color>
<color name="color2">#7f26db</color>
<color name="color3">#1d38df</color>
<color name="color4">#1b1b1d</color>
<color name="color5">#232428</color>

And i am trying to use them by using following code: 我正在尝试通过使用以下代码来使用它们:

textView.setTextColor(getResource().getColor(R.color.color1)); textView.setTextColor(的getResource()的getColor(R.color.color1)。);

But during execution some of them are not working. 但是在执行过程中,其中一些无法正常工作。 like color1 is not working. 就像color1无法正常工作。

Its throwing exception :- String #e0195c conversion error 它的抛出异常:-字符串#e0195c转换错误

Please let me know if i am missing something. 如果我想念什么,请告诉我。

You can set the color dynamically like 您可以像

textview.setTextColor(Color.parseColor("#e0195c"));

Its working for me. 它为我工作。 And i am not getting any Exception. 而且我没有得到任何例外。

Now i created an Array for Color code 现在我为颜色代码创建了一个数组

private String[] colorHexCode = {"#e0195c", "#7f26db", "#1d38df"}; 

textview.setTextColor(Color.parseColor(colorHexCode[0])); 

So you can change the color by using array also. 因此,您也可以使用数组来更改颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM