我正在这样的代码中设置 ListView 项目的背景:
RelativeLayout root;
root = (RelativeLayout) convertView.findViewById(R.id.root);
root.setBackgroundColor(-14774017);
这会正确设置背景颜色,但不透明度为 100%。我想设置相对布局背景的透明度。我知道十六进制代码可以在 android 的开头有 alpha 值 - 例如 #AARRGGBB
,但是当我使用整数颜色值时,如何为背景添加透明度(例如 -14774017
)?
最佳答案
color int 值包含所有 alpha、红色、绿色和蓝色分量。
The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution.
Color
类提供了提取或组合这些组件的实用方法。以下代码段将从用户指定的颜色和 alpha 值创建一个颜色 int
:
int alpha = 128; //50% transparency
int color = -14774017; //Your color value
int bgColor = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
root.setBackgroundColor(bgColor);
关于android - 在代码中设置listview项的透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18830254/