开发者

How to add a second drop down list to Action Bar in Android Honeycomb?

开发者 https://www.devze.com 2023-02-27 00:51 出处:网络
I\'ve been playing with Honeycomb for the last week and had a hard time to开发者_开发问答 find something about more than one drop-down list in Action Bar.

I've been playing with Honeycomb for the last week and had a hard time to开发者_开发问答 find something about more than one drop-down list in Action Bar. I've used this tutorial and successfully added one drop-down list to it. But I don't know how to add another one. Please tell me if you know how to do it. Thanks.

P.S. also one little this how can I change the position of elements in Action Bar(this is off topic)?


Not sure about Honeycomb but I was able to add two drop down lists to my action bar in JellyBean without using a custom view.

res/menu/main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:id="@+id/menu_main_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:showAsAction="always"/>
  <item
    android:id="@+id/menu_status_spinner"
    android:actionViewClass="android.widget.Spinner"
    android:showAsAction="always"/>
</menu>

MyActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate( R.menu.main_menu, menu );

    MenuItem mainMenuSpinner = menu.findItem( R.id.menu_main_spinner);
    setupMainMenuSpinner( mainMenuSpinner );

    MenuItem statusSpinner = menu.findItem( R.id.menu_status_spinner );
    setupStatusSpinner( statusSpinner );

    return super.onCreateOptionsMenu(menu);
}

private void setupMainMenuSpinner(MenuItem item) {
    View view = item.getActionView();
    if (view instanceof Spinner) {
        Spinner spinner = (Spinner) view;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.main_menu_options,
                android.R.layout.simple_spinner_dropdown_item));
    }
}

private void setupStatusSpinner(MenuItem item) {
    View view = item.getActionView();
    if (view instanceof Spinner) {
        Spinner spinner = (Spinner) view;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.status,
                android.R.layout.simple_spinner_dropdown_item));
    }
}


You would have to do that yourself. Instead of setNavigationMode(), use setCustomView().

P.S. also one little this how can I change the position of elements in Action Bar(this is off topic)?

Generally, you can't. You control the order (e.g., toolbar buttons via the order they are in your options menu XML). That's pretty much it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号