我這樣做如下:我已經使用了getDropDownView()
和getView()
方法。
使用getDropDownView()
打開Spinner
。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
if (!((LabelItem) getItem(position)).isEnabled()) {
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item_disabled));
} else {
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item));
}
return view;
}
而且使用getView()
封閉式Spinner
。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.context_row_icon, null);
}
TextView mTitle = (TextView) view.findViewById(R.id.context_label);
ImageView flag = (ImageView) view.findViewById(R.id.context_icon);
mTitle.setText(values[position].getLabel(activity));
mTitle.setTextColor(activity.getResources().getColor(
R.color.context_item_disabled));
return view;
}
spinner1 in「spinner1 = new ArrayAdapter(this,R.layout.spinner_item,array);」應該是mAdapter。 –
Daniel