列表选择框之---Spinner

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

5.

6.

7.

8.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容