问题
Can I make the items to show up in alternating colors in spinner control
回答1:
Yes, you can. If you are following the Spinner Tutorial then you can subclass your adapter (passing a List
of your items) to produce an alternating color effect:
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter(this, R.layout.simple_spinner_item, myList) {
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
if (position % 2 == 0) { // we're on an even row
view.setBackgroundColor(evenColor);
} else {
view.setBackgroundColor(oddColor);
}
return view;
}
}
If you're already defining your own adapter and implementing getDropDownView
then you can add something like the above to the end of your method.
Edit - updated with slund's advice. Thank you!
回答2:
You can create a custom adapter and change the color of each item. Look at this link for more details.
来源:https://stackoverflow.com/questions/5447009/alternating-colors-of-spinner-items