一个下拉列表的实现,这里记录下,下拉列表是如何实现的:
(1)布局:
<Spinner
android:id="@+id/sp_xinjian"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
(2)Item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_changneizuoye_xinjian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="内容" />
</LinearLayout>
(3)Activity中:
private Spinner sp_xinjian;
sp_xinjian = (Spinner) findViewById(R.id.sp_xinjian);
// 给Adapter填充数据,mZhiLing为String数组,这里不写里,内容自己准备就行
adapter_zhiling = new ArrayAdapter<String>(
getApplicationContext(),
R.layout.changneizuoye_xianjian_item,
R.id.tv_changneizuoye_xinjian, mZhiLing);
// 设置适配器,显示
sp_xinjian.setAdapter(adapter_zhiling);
(4)(根据需要)默认显示:
如果想要默认显示哪一项内容的话,可以再加一句:
//i为数组的下标
sp_xinjian.setSelection(i, true);
(5)(根据需要)样式:
下拉列表的样式是可以自己更改的,需要写个styles,这里不介绍了。
转载于:https://blog.51cto.com/liuxudong1001/1734551