android - 在代码中设置listview项的透明度

标签 android listview

我正在这样的代码中设置 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/

相关文章:

android - SMP 与 Android 的集成

android - 将 appbarLayout 放在屏幕底部

java - 如何使用Retrofit 2.0从其他线程获得响应

java - Android ListView显示位置错误

android - 如何在 edittext + listview 布局中隐藏键盘

android - 使用蓝牙和应用程序打开/关闭设备

android - 如何通过单击按钮在 ListView 中获取所选项目

android - 编辑列表查看内容 - Android

android - 根据其中的多行 TextView 动态设置 ListView 高度

android - ACTION_DOWN、ACTION_UP 和 onClick 事件