Action Bar - ListAction

1 篇文章 / 0 new
author
Action Bar - ListAction
►在 bar 上呈現 下拉選單 操作模式
ActionBar aBar = getActionBar();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, new String[] {"List Item 1","List Item 2"});
aBar.setListNavigationCallbacks(adapter, new ActionBar.OnNavigationListener() {
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        return false;
    }
});
aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
使用 setListNavigationCallbacks() 方式則選單會顯示在 view control的位置, 非 Action button區域內.

► 若想在 action button區域內也呈現下拉選單則作法如下(注意 spinner在 overflow區內不能正確運作)
menu item 加入一個 spinner class itme
<item<br />
      android:id="@+id/menu_spinner"<br />
      android:actionViewClass="android.widget.Spinner"<br />
      android:showAsAction="always"/>//需設為 always 否則會因自動轉型為SearchView而出錯

spinner 設定
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    //下拉式選單
    Spinner spinner = (Spinner) menu.findItem(R.id.menu_spinner).getActionView();
    spinner.setAdapter( ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_dropdown_item ) );
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
Free Web Hosting