Android 如何为列表视图中的每一行设置不同的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10479108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set different background color for each row in listview?
提问by Samir Mangroliya
采纳答案by Herry
As you said that you have use Custom adapter for listview then what you need to do is below.
in getView
method of your adapter you need to set background color of your list row xml's parent view.
正如您所说,您已将自定义适配器用于列表视图,那么您需要做的如下。在getView
您的适配器方法中,您需要设置列表行 xml 父视图的背景颜色。
回答by Samir Mangroliya
in getView(...) method
在 getView(...) method
if (position == 0) {
view.setBackgroundResource(R.drawable.bg_list_even);
} else if (position == 1) {
view.setBackgroundResource(R.drawable.bg_list_odd);
} else...
Update::
更新::
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title = (TextView) view.findViewById(R.id.txttitle);
holder.description = (TextView) view.findViewById(R.id.txtdesc);
holder.title.setText("Title" + position);
holder.description.setText("Desc" + position);
//here set your color as per position
if (position == 0) {
view.setBackgroundResource(R.drawable.bg_list_even);
} else if (position == 1) {
view.setBackgroundResource(R.drawable.bg_list_odd);
}
return view;
}
holder class
持有人等级
public class ViewHolder {
public TextView title;
public TextView description;
}
回答by Khan
Make an array as given below as no of list item i suppose u have five items
制作一个如下所示的数组作为列表项的编号,我想你有五个项目
int[] color_arr={Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GREEN,Color.RED};
and after do in ur getView method of custome adapter as below
然后在你的客户适配器的 getView 方法中做如下
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row=convertView;
row = inflater.inflate(R.layout.listview_custome, parent, false);
row.setBackgroundColor(color_arr[position]);// this set background color
TextView textview = (TextView) row.findViewById(R.id.tv_list);
ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
return (row);
}
回答by vijaycaimi
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = convertView;
rowView = inflater.inflate(R.layout.listview_custome, parent, false);
rowView.setBackgroundColor(color_arr[position]);// this set background color
TextView textview = (TextView) rowView.findViewById(R.id.tv_list);
ImageView imageview = (ImageView) rowView.findViewById(R.id.iv_list);
textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);
if (position == 0) {
rowView.setBackgroundColor(Color.BLUE);
}
else if (position % 2 == 1) {
rowView.setBackgroundColor(Color.RED);
}
else if (position % 2 == 0) {
rowView.setBackgroundColor(Color.BLUE);
}
return (rowView);
}