5

I'm using Actionbar Sherlock. The activity be shown on startup should start in a "search mode" to start searching immediately. For doing so I use the following code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    //collapse search
    MenuItem searchItem = menu.add(Menu.NONE, R.string.inlineSearch, Menu.NONE, getString(R.string.inlineSearch)).setIcon(R.drawable.menu_search);
    searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    searchView = new SearchView(this);
    searchItem.setActionView(new SearchView(this));
    searchItem.expandActionView();
    return true;
}

The SearchView is the View provided by Android / Actionbar Sherlock.

The problem I'm facing with that is that no matter what I do, the item is never expanded on startup. I tried calling the expandActionView method after startup by using another actionbar item, nothing changed. I implemented my own View implementing CollapsibleActionView, but the methods onActionViewExpanded() and onActionViewCollapsed() never get called.

But if I click the collapsed button of the SearchView, the view expands as expected.

Does anyone know what I'm doing wrong? Thanks for your help!

4

1 回答 1

14

在浏览了源文档之后,我终于自己找到了答案。 menuItem.expandActionView()只有在 MenuItem 设置了以下标志时才会生效:searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

希望对那里的人有所帮助!

于 2012-06-14T12:55:15.117 回答