alternating colors of spinner items

浪子不回头ぞ 提交于 2019-12-08 05:22:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!