一、普通
1、
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:gravity="left" android:paddingLeft="8dip" android:singleLine="true" android:textSize="12sp" android:textStyle="bold" android:textColor="@color/gsdk_black" android:text="sdf" />
2
private String[] method = null; ArrayAdapter<String> methodAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, method);
methodSpinner = (Spinner) findViewById(R.id.spinner_recharge_type); moneyAdapter
.setDropDownViewResource(android.R.layout.select_dialog_item); methodSpinner.setAdapter(methodAdapter);
二、自定义spinner:
1、配置文件:
<?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="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_spinner_id" android:layout_width="60dp" android:layout_height="80dp" android:visibility="gone" /> <TextView android:id="@+id/tv_spinner_name" style="?android:attr/spinnerDropDownItemStyle" android:layout_width="match_parent" android:layout_height="35dip" android:gravity="center_vertical" android:paddingLeft="10dp" android:textColor="#000" android:textSize="@dimen/font_body" /> </LinearLayout>
2、填充数据
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); for (Project project : linkedList) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", project.getName()); map.put("id", project.getId()); data.add(map); } searchAdapter = new SimpleAdapter(PurchaseActivity.this, data, R.layout.purchase_search_spinner, new String[]{"id","name"}, new int[]{R.id.tv_spinner_id,R.id.tv_spinner_name}); searchSpinner.setAdapter(searchAdapter);
3、点击事件:注意点击事件是OnItemSelectedListener而不是OnItemClickListener.
private class SearchSpinnerItemListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Spinner spinner = (Spinner) parent; Map<String, Object> projectMap = (Map<String, Object>) spinner.getItemAtPosition(position); int pid = Integer.valueOf(projectMap.get("id")+""); String pname = (String) projectMap.get("name"); Log.i("Purchase","pid: "+pid+" pname: "+pname); } @Override public void onNothingSelected(AdapterView<?> parent) { } }