如何在 Android 中将 Int 值转换为 COLOR(使用 Kotlin 或 Java)?

Alp*_*lan 0 graphics android decimal colors kotlin

我搜索了很多但找不到解决方案。

从 API 中,它返回一个 Int 作为颜色值 2813300。这是 Green LiNK to COLOR的色调

我尝试在我的项目中使用此 Int 作为颜色,但无法将其转换为 android.graphics.Color。

当我尝试使用 int 时,它不起作用。

如何将这些数字转换为颜色?

例子:

红色 = 16711936(链接

绿色 = 2813300(链接

ane*_*hme 5

Integer.toString 会将您的 int 值转换为 Hex 值

 Integer.toString(2813300, 16)
Run Code Online (Sandbox Code Playgroud)

结果 :

2AED74
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用十六进制颜色:

String Hex  = Integer.toString(2813300, 16);
myLayout.setBackgroundColor(Color.parseColor("#" + Hex));
Run Code Online (Sandbox Code Playgroud)