Android设置后台资源和颜色

mee*_*epz 3 android

是否有可能设置backgroundResourcebackgroundColor一个自定义适配器的ListView/ExpandableListView的.

我有一个透明的png,它将作为可扩展列表视图中每行的边框.此图像只是内部阴影和底部边框.

目标是做这样的事情:

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;
}
Run Code Online (Sandbox Code Playgroud)

nan*_*esh 9

setBackgroundResource并且setBackgroundColor都在setBackgroundDrawable内部使用相同的api 来完成他们的任务.所以一个人会覆盖另一个人.因此,您无法使用此api实现目标.

您必须使用setBackgroundResource和自定义drawable


len*_*ooh 5

如果您想使用setBackgroundResource并且setBackgroundColor可以这样做:

...
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
GradientDrawable drawable = (GradientDrawable) convertView.getBackground();
drawable.setColor(color2[colorPos]);
...
Run Code Online (Sandbox Code Playgroud)