Android的替代行的颜色中的ListView颜色、Android、ListView

2023-09-05 08:38:46 作者:丿皇族灬爱恋丶

public class ListView extends  ListActivity {

static String item;

public void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, Str.S);
            setListAdapter(adapter);

      }

这是我这工作不错列表视图类,它需要琴弦一类被称为海峡,并在列表视图显示出来,问题是列表视图风格不是很好,它是黑色与白色的字符串。

This is my list view class which works nice and it takes the strings from a class called Str and display them in a listview, the problem is the listview style isn't nice, it's black with the strings in white.

我想他们是另类的每一行都有一个颜色。

I want them to be alternative each row has a color.

我试过很多教程,但没有一个人清楚.. 如何使另类色彩的每一行..恩。 ROW1蓝,行2白,排3蓝,4行白色等。

I tried many tutorials but none was clear enough .. How do I make Alternative Color for each row .. ex. row1 Blue, row 2 White, row 3 Blue, row 4 White, etc..

推荐答案

的这里是如何做到这一点。

我的例子code在这里给出了简短的:

My example code is given here in brief:

在您的适配器重写 getView 方法:

Override the getView method in your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {  
View view = super.getView(position, convertView, parent);  
if (position % 2 == 1) {
    view.setBackgroundColor(Color.BLUE);  
} else {
    view.setBackgroundColor(Color.CYAN);  
}

return view;  
}

覆盖 ArrayAdapter 并重写getView方法那里。

Override ArrayAdapter and override getView method there.

所以,如果您的适配器是这样的:

So if your adapter is something like this:

public class MyAdapter extends ArrayAdapter

ListActivity 将改变这样的:

 ArrayAdapter<String> adapter = new MyAdapter<String>(this,
                android.R.layout.simple_list_item_1, Str.S);

下面是一个例子有关覆盖ArrayAdapter。

Here's an example about overriding ArrayAdapter.