Android - Spinner

Android - Spinner

這次要來實作Spinner

參考文章

http://givemepass.blogspot.tw/2011/11/spinner.html

先準備選單字串陣列,及layout放入Spinner ,

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

private Spinner spinner_menu;
private String[] spinner_list;
private ArrayAdapter<String> spinner_adapter;

private void initSpinner() {
      spinner_menu = (Spinner) findViewById(R.id.spinner_menu);
      spinner_adapter=  new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_list);
      spinner_menu .setAdapter(spinner_adapter);
      spinner_menu .setOnItemSelectedListener(mSpinnertemSel);
      spinner_menu .setSelection(spinner_list.length - 1);
} 

private AdapterView.OnItemSelectedListener mSpinnerItemSel = new AdapterView.OnItemSelectedListener(){
      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
           String spinner_val = spinner_list[position];
      }
     @Override
     public void onNothingSelected(AdapterView<?> arg0) {

     }
};






額外參考

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2264.html

議題:

改成下拉選單方式

android:spinnerMode="dropdown"

變更下拉選單背景

android:popupBackground="@drawable/bg_spinner_dropdown"

位移下拉選單位置,蓋過原本按鈕上方

android:dropDownVerticalOffset="-40dp"

下拉選單,列表線的顏色

請將原本的item的TextView 設置background 放底線處理