以十六进制获取视图的背景颜色

Ali*_*ali 3 user-interface android background view colors

我想以十六进制格式获取视图的背景颜色。

例如,考虑int getViewBackgroundColor(View view)我的例外返回值是0Xff256e78.

我怎么能这样做?

谢谢。

Nik*_*rad 5

LinearLayout layout = (LinearLayout) findViewById(R.id.lay1);
ColorDrawable viewColor = (ColorDrawable) layout.getBackground();
int colorId = viewColor.getColor();
Run Code Online (Sandbox Code Playgroud)

获得整数类型的颜色后,现在必须转换为十六进制:

String hexColor = String.format("#%06X", (0xFFFFFF & colorId));
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助..