Android设置后台资源和颜色

时间:2012-08-15 21:07:05

标签: android

是否可以在自定义适配器中设置ListView / ExpandableListView的backgroundResourcebackgroundColor

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

目标是做这样的事情:

png + color

当我设置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;
}

4 个答案:

答案 0 :(得分:7)

setBackgroundResourcesetBackgroundColor都在内部使用相同的api setBackgroundDrawable来执行任务。所以一个人会覆盖另一个人。因此,您无法使用此api实现目标。

您必须将setBackgroundResource与自定义可绘制

一起使用

答案 1 :(得分:4)

如果您想使用setBackgroundResourcesetBackgroundColor,可以执行此操作:

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

答案 2 :(得分:1)

确保代码可以采用背景绘制并使用油漆颜色。但我现在不能报名:-)
但是,还有其他方法可以实现您的目标。看看这个网站:
http://developer.android.com/guide/topics/resources/drawable-resource.html
看看你可以准备的9patch drawables,这将只是根据需要收缩/扩展的小图像。你在里面准备一种颜色和其他颜色 第二种方法是使用ShapeDrawable。在XML中,您可以在其中创建矩形和一些纯色 在这两种情况下,您只需根据需要交换背景绘图 我不明白你想要什么,但我希望这会有所帮助。

答案 3 :(得分:0)

将背景资源设置为这种视图之后,只需添加一个ColorFilter

select s.*, f.*
from (
    select 
        s.*, 
        row_number() over(
            partition by shift_start, shift_end, shift_function 
            order by shift_id
        ) rn
    from shift s
    where shift_day = @shift_day
) s
left join (
    select 
        c.*,
        row_number() over(
            partition by schedule_start, schedule_end, schedule_function 
            order by schedule_id
        ) rn
    from schedule c
    where schedule_date = @schedule_date
) c 
    on  c.schedule_start = s.shift_start 
    and c.schedule_end = s.shift_end
    and c.schedule_function_id = s.shift_function_id
    and c.rn = s.rn
left join function f
    on f.function_id = s.shift_function_id
where c.rn is null
order by f.function_name, s.shift_start, s.shift_end

详细了解Manipulating images and Drawables with Android’s ColorFilter

相关问题