android: each item of spinner different color

廉价感情. 提交于 2019-12-06 13:58:52

create your own class that extends BaseAdapter and implements SpinnerAdapter.

Override getDropDownView, and as you process the position you can format the textview from a custom layout you inflate.

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){

    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.your_custom_layout, null);
    }
    TextView tv=(TextView) v.findViewById(R.id.yourTextViewFromYourLayout);
    tv.setText(yourArrayList.getItem(position));
    switch (position) {
case 0:
 //set tv's color here...
 break;
case 1:
 //set tv's color here...
etc...
default:
 //set default color or whatever...
}       
        return v;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!