要更改安卓列表视图的颜色,您可以尝试以下方法:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setBackgroundColor(Color.RED); // 设置列表项的背景颜色为红色
TextView textView = view.findViewById(android.R.id.text1);
textView.setTextColor(Color.BLUE); // 设置列表项的文字颜色为蓝色
return view;
}
};
public class CustomAdapter extends ArrayAdapter {
private Context context;
private List items;
public CustomAdapter(Context context, List items) {
super(context, 0, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.custom_list_item, parent, false);
}
String item = items.get(position);
TextView textView = view.findViewById(R.id.textView);
textView.setText(item);
if (position % 2 == 0) {
view.setBackgroundColor(Color.RED); // 设置偶数位置的列表项的背景颜色为红色
textView.setTextColor(Color.BLUE); // 设置偶数位置的列表项的文字颜色为蓝色
} else {
view.setBackgroundColor(Color.GREEN); // 设置奇数位置的列表项的背景颜色为绿色
textView.setTextColor(Color.YELLOW); // 设置奇数位置的列表项的文字颜色为黄色
}
return view;
}
}
请注意,上述代码示例仅为演示目的,您需要根据您的具体需求进行适当的修改。
下一篇:安卓列表视图展示