Android下拉框控件Spinner的使用

本文介绍了在Android中实现下拉框(Spinner)的两种方法:一种是通过自定义BaseAdapter来创建复杂的下拉项视图;另一种是使用ArrayAdapter快速实现简单的下拉列表。此外,还探讨了如何自定义Spinner的外观,包括设置不同的状态样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面看一下在android里面一个下拉框的简单应用,首先定义个xml文件:

select_spinner.xml

<?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/TextView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="this is FirstActivity of AppB"
    />

    <Spinner 
    android:id="@+id/spinner" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
  
</LinearLayout>

其次在画面里面可以这样实现(方式一):

 protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_spinner);
        
        
        
        
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setPrompt("请选择");
        final int [] drawableIds = {R.drawable.aa, R.drawable.icon};
        final int [] msgIds = {R.string.aa, R.string.bb};
        
        
        
        BaseAdapter baseAdapter = new BaseAdapter() {

            @Override
            public int getCount() {
                return msgIds.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
             // 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout
             // 中包含一个ImageView及一个TextView构成
             // 初始化LinearLayout
             LinearLayout ll = new LinearLayout(TestActivity.this);
             ll.setOrientation(LinearLayout.HORIZONTAL);
             // 初始化ImageView
             ImageView ii = new ImageView(TestActivity.this);
             ii.setImageDrawable((getResources().getDrawable(drawableIds[position])));
             ll.addView(ii);
             // 初始化TextView
             TextView tv = new TextView(TestActivity.this);
             tv.setText(" " + getResources().getText(msgIds[position]));
             tv.setTextColor(R.color.blue);
             tv.setTextSize(24);
             ll.addView(tv);
             return ll;
            }
            
        };
        
        
        spinner.setAdapter(baseAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
            @Override 
            public void onItemSelected(AdapterView<?> arg0, View view, 
                    int arg2, long arg3) { 
                //被选中时候发生的动作
                Log.i("chen", "===="+arg0+"   "+view+"    "+arg2+"    "+arg3);
                
             // 获取主界面TextView
                TextView tv = (TextView) findViewById(R.id.TextView01);
                // 获取当前选中选项对应的LinearLayout
                LinearLayout ll = (LinearLayout) view;
                // 获取其中的TextView
                TextView tvn = (TextView) ll.getChildAt(1);
                // 用StringBuilder动态生成信息
                StringBuilder sb = new StringBuilder();
                sb.append(getResources().getText(R.string.app_name));
                sb.append(":");
                sb.append(tvn.getText());
                // 信息设置进住界面
                tv.setText(sb.toString());
            } 
            @Override 
            public void onNothingSelected(AdapterView<?> arg0) { 
                
            } 
        });
    }

如果用ArrayAdapter实现如下(方式二):

//设置下拉框控件的标题文本内容:
spinner.setPrompt("请选择");

//定义下拉框内容:
String [] datas = {“ATAAW.COM”,”Android.com”,”Google.com”};

//定义添加适配器:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( 
this, datas, android.R.layout.simple_spinner_item); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinner.setAdapter(adapter);

//响应下拉框的选中值发生变化的事件处理:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
            @Override 
            public void onItemSelected(AdapterView<?> arg0, View arg1, 
                    int arg2, long arg3) { 
                //被选中时候发生的动作 
            } 
            @Override 
            public void onNothingSelected(AdapterView<?> arg0) { 
            } 
        });

第二种方式稍微试了下,就是android.R.layout.simple_spinner_item这个样式不可控,和其他控件风格有冲突,不好用。

所以得想想如何自定义spinner:

1、首先可以准备两张图片,一张是显示状态spinner.png,一张是按下时的状态spinner_press.png

2、在drawable中定义spinner_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"   
        android:drawable="@drawable/spinner_press" /><!--按下时效果-->  
    <item android:state_pressed="false"   
        android:drawable="@drawable/spinner" /><!--默认效果-->   
</selector>

3、

style中定义
      <!-- spinner -->
      <style name=" spinner_style">
      <item name="android:background"> @drawable/spinner_selector</item>
      <item name="android:paddingLeft">5dip</item>
      </style>

4、调用
<Spinner
      android:id="@+id/field_item_spinner_content"
      style=" @style/spinner_style"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:animationCache="true"
      android:drawSelectorOnTop="true" />

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值