1. 在布局文件中使用
<Spinner
android:id="@+id/spanner_business"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:entries="@array/數(shù)組名稱(chēng)"
android:paddingLeft="@dimen/y2"
android:textSize="@dimen/x13"/>
2. 在代碼中添加列表數(shù)據(jù)
final String[] arr1 = new String[]{"互聯(lián)網(wǎng)", "金融", "餐飲", "教育", "影視娛樂(lè)", "農(nóng)林牧業(yè)", "地產(chǎn)建筑", "旅游酒店", "工業(yè)制造", "其它"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spnner_text, arr1);
spannerBusiness.setAdapter(adapter);
spannerBusiness.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
businessType = arr1[position];
TextView tv = (TextView)view;
tv.setGravity(android.view.Gravity.CENTER_HORIZONTAL); //設(shè)置居中
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
3. 在xml中編寫(xiě)數(shù)組資源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>互聯(lián)網(wǎng)</item>
<item>金融</item>
<item>餐飲</item>
<item>農(nóng)業(yè)</item>
<item>地產(chǎn)</item>
</string-array>
</resources>
4. 大神的博客
:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2264.html