当我使用此代码时:
spinner_times_adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, times_array);
spinner_times_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_time.setAdapter(spinner_times_adapter);
我在下拉视图中得到了合适的高度。
但是当我使用这样的自定义适配器时:
spinner_dates_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dates_array){
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) super.getView(position, convertView, parent);
boolean red = isRed(position);
if(red){tv.setTextColor(Color.RED);}
else{tv.setTextColor(Color.WHITE);}
return tv;
}
public boolean isRed(int position) {
String val = dateBools_array.get(position);
boolean isRedBool;
if(val.equals("0")){
isRedBool = true;
}
else{
isRedBool = false;
}
return isRedBool;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = this.getContext();
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Androids orginal spinner view item
v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
// The text view of the spinner list view
TextView tv = (TextView) v.findViewById(android.R.id.text1);
String val = dates_array.get(position);
// remove the extra text here
tv.setText(val.replace(":RED", ""));
boolean red = isRed(position);
if(red){tv.setTextColor(Color.RED);}
else{tv.setTextColor(Color.WHITE);}
return v;
}
};
spinner_dates_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_date.setAdapter(spinner_dates_adapter);
项目之间不再有空白空间,这使得很难在手机上选择合适的项目。
我在做什么错?如何在下拉视图中增加每个项目的高度,以便用户可以进行适当的选择?