更新时间: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.