在Android SDK使用Spinner工程案例:
首先,你要创建自定义的适配器,拦截下拉列表创建的视图:
static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
public CustomArrayAdapter(Context ctx, T [] objects)
{
super(ctx, android.R.layout.simple_spinner_item, objects);
}
//其它构造函数
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
// simple_spinner_item 有 android.R.id.text1 TextView视图:
/* if(isDroidX) {*/
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);//choose your color :)
/*}*/
return view;
}
}
然后在你的代码中创建适配器:
String [] spin_arry = getResources().getStringArray(R.array.Planets);
this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);
因为CustomArrayAdapter
知道我使用android内置布局资源,也知道TextView视图中的文本会被使用idandroid.R.id.text1
代替。这就是为什么它会拦截下拉列表创建的视图,然后变成想要的颜色。
屏幕截图: