且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android-以编程方式获取listview(item)的背景色

更新时间:2022-12-26 19:58:12

如果要从ListView获取视图背景色

If you want to obtain a view background colour, from a ListView

ListView listView = (ListView) findViewById(R.id.my_list);

获得背景尝试

Drawable listDrawableBackground = listView.getBackground();

如果背景是一种颜色,要获得该颜色,您可以尝试

if background is a colour, to obtain the colour, you can try

ColorDrawable listViewColor = (ColorDrawable) listView.getBackground();

要获取实际颜色,请使用

to get the actual colour, use

int colorId = listViewColor.getColor();

您可以将此colorId与现有颜色进行比较,如下所示:

you can compare this colorId with existing color like so:

if(colorID == R.color.blue){Log(颜色为蓝色");}

另一种方法是将标签添加到ListView,标签将是颜色,例如

Another way will be to add a tag to the ListView, and the tag will be the color, say

tag ="#ccc",您可以使用

tag = "#ccc", and you can use

String colorCode = (String)listView.getTag();

现在您有了colorCode,并且可以在程序中的任何地方重复使用它.

now you have the colorCode and you can reuse it anywhere in your program.