一、Android自带的spinner开发,即不带图片的spiner
1、创建布局:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/books"
/>
2、创建spinner条目,在values文件夹中创建 array.xml:
<resources>
<array name="books">
<item>Java</item>
<item>Java for mail</item>
<item>Java for Android</item>
</array>
</resources>
3、在 MainActivity 中实现代码:
String[] books2 = getResources().getStringArray(R.array.books);
ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, books2);
Spinner spinner = new Spinner(getApplicationContext());
spinner.setAdapter(aa2);
二、自定义的spinner
1、只修改baseAdapter中的 getView() 代码
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(0);
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text = new TextView(MainActivity.this);
text.setText(books2[position]);
line.addView(image);
line.addView(text);
return line;
}