Android:如何为微调器项目提供替代颜色

时间:2013-02-25 09:54:16

标签: android android-spinner

我有一个带有以下项目的微调器

ArrayAdapter<Message> arrayadapter = new ArrayAdapter<Message>(activity, android.R.layout.simple_spinner_item, messages);
                arrayadapter.setDropDownViewResource(R.layout.textview);

我想给listitems提供altenate颜色。我可以这样做。感谢任何帮助

3 个答案:

答案 0 :(得分:2)

您可以使用

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;
    }
}

答案 1 :(得分:1)

答案 2 :(得分:0)

您好您可以使用以下代码

 @Override
       public View getDropDownView(int position, View convertView, ViewGroup parent){
            View v = convertView;
            if (v == null) {
               Context mContext = this.getContext();
               LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
         } 
    TextView tv=(TextView) v.findViewById(R.id.spinnerTarget);
    tv.setText(testarray.get(position));
    switch (position) {
    case 0:  tv.setTextColor(Color.RED);  
    break; 
    case 1:  tv.setTextColor(Color.BLUE);
    break;
    default:  tv.setTextColor(Color.BLACK);
    break;
    }
return v;  
              }              
       };     
       pSpinner.setAdapter(spinnerAdapter); 
}