简体   繁体   English

Android将Int转换为Hex颜色

[英]Android Convert Int to Hex Color

How can I convert an int to a hex color and use it with a ColorDrawable ? 如何将int转换为十六进制颜色并将其与ColorDrawable一起使用?

I have tried other methods that use a string, and I get "ColorDrawable cannot be applied to java.lang.String". 我已经尝试过使用字符串的其他方法,并且我得到“ColorDrawable不能应用于java.lang.String”。

I have tried: 我努力了:

String strColor = String.format("#%06X", 0xFFFFFF & actionColor);

actionBar.setBackgroundDrawable(new ColorDrawable(strColor));

But I cannot apply that to a ColorDrawable. 但我无法将其应用于ColorDrawable。 I am trying to set the color of the ActionBar with hex. 我试图用十六进制设置ActionBar的颜色。

Thanks 谢谢

The ColorDrawable takes an int as parameter not string . ColorDrawable将int作为参数而不是string I suppose actionColor is also an integer (color hex) so you should do this. 我想actionColor也是一个整数(颜色十六进制),所以你应该这样做。

int color = 0xFFFFFF & actionColor;
actionBar.setBackgroundDrawable(new ColorDrawable(color));

You can think about a color as composed of 3 components. 您可以将颜色视为由3个组件组成。 RGB. RGB。 In your example you have 0xffffff. 在你的例子中,你有0xffffff。 The first ff is the red component, the second ff is green component the third FF is the blue component. 第一个ff是红色分量,第二个ff是绿色分量,第三个FF是蓝色分量。 Change those hexadecimal values you can get your colors. 更改那些可以获取颜色的十六进制值。

eg use 例如使用

int color = Color.rgb(255, 255, 175);

Maybe I am not fully understanding the question, but if I'm getting it correctly, depending on your application purposes perhaps you could just use the int directly as follows: 也许我并没有完全理解这个问题,但是如果我正确地理解它,根据你的应用目的,也许你可以直接使用int,如下所示:

   actionBar.setBackgroundColor(actionColor);

Assuming actionColor has the color you need, the function will take the int in its "hex form" and know what to do. 假设actionColor具有您需要的颜色,该函数将以“十六进制形式”获取int,并知道该怎么做。

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

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