Spinner就是下拉框组件,可以自定义下拉布局样式,可以使用ArrayAdapter以及SpinnerAdapter适配
在Adapter中实现SpinnerAdapter,继承BaseAdapter类
private class ListAdapter extends BaseAdapter implements SpinnerAdapter {
@Override
public int getCount() {
return allLists.size();
}
@Override
public Object getItem(int position) {
return allLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(allLists.get(position).getName());
return text;
}
}
然后它不需要重写所有的像isEmpty(), registerDataObserver()这样的方法,但是可以重写getDropDownView(...)方法.
例子:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 定义了一个Spinner组件, 指定该显示该Spinner组件的数组 --> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/sanguo" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="sanguo"> <item>魏</item> <item>蜀</item> <item>吳</item> </string-array> </resources>