Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)

2019-12-10 19:59:10王旭

下面是关于Spinner的点击事件(效果图如上图):

 

 
  1. mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {  @Override 
  2. public void onItemSelected(AdapterView<?> parent, View view,  int position, long id) { 
  3. String str=parent.getItemAtPosition(position).toString();  Toast.makeText(SpinnerActivity.this, "你点击的是:"+str, 2000).show(); 
  4. }  @Override 
  5. public void onNothingSelected(AdapterView<?> parent) {  // TODO Auto-generated method stub 
  6. }  }); 

(二)使用自定义的Adapter(重点)

①:定义每一个Item的布局文件

 

 
  1. <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  2. android:layout_width="match_parent"  android:layout_height="match_parent" 
  3. android:orientation="horizontal" >  <TextView 
  4. android:id="@+id/textView1"  android:layout_width="wrap_content" 
  5. android:layout_height="wrap_content"  android:drawableLeft="@drawable/ic_launcher" 
  6. android:paddingRight="8dip"  android:paddingTop="8dip" 
  7. android:text="TextView"  android:textSize="25sp" /> 
  8. <TextView  android:id="@+id/textView2" 
  9. android:layout_width="wrap_content"  android:layout_height="wrap_content"