我想为每一行添加渐变。我试图做的是在listview标签把这个attribut,android:background =“@ drawable/mygradient”。但我得到的是这个渐变显示在列表中。我想要的是为每个项目显示此渐变。Android:ListView为每一行添加渐变
在此先感谢
我想为每一行添加渐变。我试图做的是在listview标签把这个attribut,android:background =“@ drawable/mygradient”。但我得到的是这个渐变显示在列表中。我想要的是为每个项目显示此渐变。Android:ListView为每一行添加渐变
在此先感谢
您可以提供自定义适配器您列表视图并将梯度设置为其getView
方法中的背景。事情是这样的:
public class MyAdapter extends BaseAdapter {
List<MyDataType> myData;
Context context;
public MyAdaptor(Context ctx, List<MyDataType> myData) {
this.myData = myData;
this.context = ctx;
}
public int getCount() { return myData.size() };
public Object getItem(int pos) { return myData.get(pos); }
public long getItemId(int pos) { return 0L; }
public View getView(int pos, View convertView, ViewGroup parent) {
//this is where you can customise the display of an individual
//list item by setting its background, etc, etc.
...
//and return the view for the list item at the end
return <List item view>;
}
}
然后你就可以把这个适配器作为适配器列表:
ListView myList = <initialise it here>
myList.setAdapter(new MyAdapter(getContext(), listData);
现在只要一个列表项需要显示,getView
方法将被调用,在那里你”将执行所有必要的显示定制,包括设置背景。
它已经花了我2天,但最终我设法做到了。我不习惯自定义适配器,所以它很困难。无论如何,感谢您的帮助,它非常有用。 – user1139368 2012-01-30 21:44:29
并且一年多之后它仍然没有被标记为答案= / – 2013-06-28 18:46:18
相反名单的设置背景属性设置选择,如下:
机器人:listSelector =“@绘制/ list_selector_background”
这似乎只有在选择某个项目时才起作用。 – sdasdadas 2013-03-01 02:58:31
你使用自定义的listview吗?尝试过的代码可能有助于给出答案,发布代码 – Abhi 2012-01-28 11:43:03
沿着列表显示..如何看待?你可以张贴截图吗? – 2012-01-28 11:43:18