是否可以在自定义适配器中设置ListView / ExpandableListView的backgroundResource
和backgroundColor
。
我有一个透明的png,它将用作可扩展列表视图中每一行的边框。该图像只是一个内部阴影,底部是边框。
目标是做这样的事情:
[当我先设置backgroundResource,然后再设置backgroundColor时,只会显示两个。我无法获得覆盖颜色的资源。有人知道这是否可能吗?
这是我的代码,可提供更好的主意:
private int[] colors2= new int[] { Color.parseColor("#e2e8e9"), Color.parseColor("#f1f2f2") };
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandlist_group_item, null);
holder.title = (TextView) convertView.findViewById(R.id.tvGroup);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
convertView.setBackgroundColor(color2[colorPos]);
holder.title.setText(group.getName());
return convertView;
}
setBackgroundResource
和setBackgroundColor
都在内部使用相同的API setBackgroundDrawable
来完成其任务。因此,一个覆盖另一个。因此,使用此api将无法实现您的目标。
您将必须使用setBackgroundResource和自定义可绘制对象
如果您想使用setBackgroundResource
和setBackgroundColor
,您可以这样做:
...
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
GradientDrawable drawable = (GradientDrawable) convertView.getBackground();
drawable.setColor(color2[colorPos]);
...
肯定有一些代码可以采用背景可绘制并在其上涂一些颜色。但是我现在不能退缩:-)但是还有其他方法可以实现目标。看这个网站:http://developer.android.com/guide/topics/resources/drawable-resource.html看一下您可以准备的9patch可绘制对象,它们只是很小的图像,可以根据需要缩小/扩展。您可以在其中准备一种颜色和其他颜色。第二种方法是使用ShapeDrawable。在XML中,您创建矩形和其中的一些纯色。在这两种情况下,您都只需要根据需要交换背景可绘制对象。我不明白您要实现的目标,但是希望对您有所帮助。
只需将背景资源设置为这样的视图后,添加一个ColorFilter
view.setBackgroundResource(R.drawable.yourRessource);
view.getBackground().setColorFilter(
Color.yourColor,
PorterDuff.Mode.DST_OVER
);
[了解有关Manipulating images and Drawables with Android’s ColorFilter的更多信息