Custom drop-down from action item (actionbarsherlock)

ぃ、小莉子 提交于 2019-12-04 01:54:52

问题


I need a custom drop-down menu attached to an action item. I managed to do the same thing for the home icon, but it seems that it's done totally different with other action items.

I know this question and answer: How to add a Dropdown item on the action bar My problem is that I want to fully customize it (I use custom fonts, will also need icons), so a submenu wouldn't suffice, I suppose. And attaching a custom Spinner as an actionLayout looks nothing like drop down.

For the home icon it looks like this:

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ArrayList<NavItem> data = new ArrayList<NavItem>();
    data.add(...);
    data.add(...);
    ActionBarNavAdapter adapter = new ActionBarNavAdapter(...);
    actionBar.setListNavigationCallbacks(adapter, new OnNavigationListener() {...});

And I could add my own design to it without sacrificing the "drop-down" thing. How to do it with other action items?

EDIT: I know that activity using NAVIGATION_MODE_LIST uses Spinner (ListNavigationCallbacks uses a SpinnerAdapter) but it looks better and I don't know where to get all those styles (if it's just that) to make it look like that. I mean:

Good -> How NAVIGATION_MODE_LIST looks: it's wrapping the list, it's attached under the home icon. Bad -> How a custom Spinner as an actionLayout looks: it matches the screen width with some additional dark background, replaces the action item icon (this is easy to change) and above all, darkens the rest of the screen. It look rather like an AlertDialog with a custom list, not a drop-down menu.

Code:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem sortItem = menu.findItem(R.id.sort);
    Spinner spinner = new Spinner(this);

    //this is the same adapter because I didn't want to spend time creating a new one
    //ActionBarNavAdapter extends SpinnerAdapter of course
    ArrayList<NavItem> data = new ArrayList<NavItem>();
    data.add(...);
    data.add(...);
    ActionBarNavAdapter adapter = new ActionBarNavAdapter(...);
    spinner.setAdapter(adapter);
    sortItem.setActionView(spinner);
    return true;
}

回答1:


Try replacing:

Spinner spinner = new Spinner(this);

with:

Spinner spinner = new Spinner(getActionBar().getThemedContext());

This will use a Context themed to match the action bar, and should help.



来源:https://stackoverflow.com/questions/12223283/custom-drop-down-from-action-item-actionbarsherlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!