1. 在布局文件中使用
<Spinner
android:id="@+id/spanner_business"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:entries="@array/数组名称"
android:paddingLeft="@dimen/y2"
android:textSize="@dimen/x13"/>
2. 在代码中添加列表数据
final String[] arr1 = new String[]{"互联网", "金融", "餐饮", "教育", "影视娱乐", "农林牧业", "地产建筑", "旅游酒店", "工业制造", "其它"};
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); //设置居中
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
3. 在xml中编写数组资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>互联网</item>
<item>金融</item>
<item>餐饮</item>
<item>农业</item>
<item>地产</item>
</string-array>
</resources>
4. 大神的博客
:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2264.html
网友评论