Spinner下拉列表
一般使用非常简单。直接上代码
1.布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#eeeeee" 6 android:orientation="vertical"> 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"> 10 11 <RelativeLayout 12 android:id="@+id/relativeLayout1" 13 android:layout_width="fill_parent" 14 android:layout_height="40dp" 15 android:background="#3366CC" 16 android:gravity="center" > 17 18 19 <Spinner 20 android:id="@+id/Title_Spinner" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_alignParentTop="true" 24 android:layout_centerHorizontal="true" /> 25 26 27 </RelativeLayout> 28 </LinearLayout> 29 30 <RelativeLayout 31 android:layout_width="match_parent" 32 android:layout_height="fill_parent" > 33 34 <TextView 35 android:id="@+id/textView1" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_centerHorizontal="true" 39 android:layout_centerVertical="true" 40 android:text="TextView" /> 41 42 </RelativeLayout> 43 44 </LinearLayout>
2.Activity
1 public class MainActivity extends Activity { 2 private Spinner mSpinner; 3 private TextView mTextView; 4 5 private List<String> listItem ; 6 7 @Override 8 public void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 getViews(); 13 initSpinner(); 14 } 15 16 private void initSpinner() { 17 getData(); 18 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,listItem); 19 //setDropDownViewResource设置弹出样式 20 // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 21 mSpinner.setAdapter(adapter); 22 } 23 private void getData(){ 24 listItem = new ArrayList<String>(); 25 for(int i=0;i<6;i++) 26 listItem.add("item_"+i); 27 } 28 29 private void getViews() { 30 mSpinner = (Spinner) findViewById(R.id.Title_Spinner); 31 mTextView = (TextView) findViewById(R.id.textView1); 32 } 33 34 @Override 35 public boolean onCreateOptionsMenu(Menu menu) { 36 getMenuInflater().inflate(R.menu.activity_main, menu); 37 return true; 38 } 39 40 }
使用adapter.setDropDownViewResource时效果是
不使用时效果:
接下来自定义Spinner样式
只需修改一下MainActivity.java
1 public class MainActivity extends Activity { 2 private Spinner mSpinner; 3 private TextView mTextView; 4 5 private List<String> listItem ; 6 7 @Override 8 public void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 getViews(); 13 initSpinner(); 14 } 15 16 private void initSpinner() { 17 getData(); 18 //使用自定义的Adapter 19 MySpinnerAdapter adapter = new MySpinnerAdapter(); 20 mSpinner.setAdapter(adapter); 21 } 22 private void getData(){ 23 listItem = new ArrayList<String>(); 24 for(int i=0;i<6;i++) 25 listItem.add("item_"+i); 26 } 27 28 private void getViews() { 29 mSpinner = (Spinner) findViewById(R.id.Title_Spinner); 30 mTextView = (TextView) findViewById(R.id.textView1); 31 } 32 33 @Override 34 public boolean onCreateOptionsMenu(Menu menu) { 35 getMenuInflater().inflate(R.menu.activity_main, menu); 36 return true; 37 } 38 39 /** 40 * 自定义Adapter 41 */ 42 class MySpinnerAdapter extends BaseAdapter{ 43 public MySpinnerAdapter() { 44 } 45 46 @Override 47 public int getCount() { 48 return listItem.size(); 49 } 50 51 @Override 52 public Object getItem(int position) { 53 return listItem.get(position); 54 } 55 56 @Override 57 public long getItemId(int position) { 58 return position; 59 } 60 61 @Override 62 public View getView(int position, View convertView, ViewGroup parent) { 63 // 动态生成每个下拉项对应的View,每个下拉项View由LinearLayout 64 // 中包含一个ImageView及一个TextView构成 65 // 初始化LinearLayout 66 LinearLayout ll = new LinearLayout(MainActivity.this); 67 ll.setOrientation(LinearLayout.HORIZONTAL); 68 // 初始化TextView 69 TextView tv = new TextView(MainActivity.this); 70 tv.setText(listItem.get(position)); 71 tv.setTextSize(18); 72 tv.setTextColor(Color.LTGRAY); 73 ll.addView(tv); 74 return ll; 75 } 76 77 } 78 }
效果。 可以自己定义一些布局