我有一个带有以下项目的微调器
ArrayAdapter<Message> arrayadapter = new ArrayAdapter<Message>(activity, android.R.layout.simple_spinner_item, messages);
arrayadapter.setDropDownViewResource(R.layout.textview);
我想给listitems提供altenate颜色。我可以这样做。感谢任何帮助
答案 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)
以下链接可能对您创建自定义微调器很有用。
1)http://adanware.blogspot.in/2012/03/android-custom-spinner-with-custom.html
2)http://www.edureka.in/blog/custom-spinner-in-android/
3)Android Spinner with different layouts for "drop down state" and "closed state"?
答案 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);
}