单击按钮添加SearchView

时间:2013-02-07 13:09:35

标签: android button android-actionbar actionbarsherlock

我宣布了SearchView:

SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("some text");

我的目标是仅在点击按钮后将此搜索视图添加到操作栏。它不是一个menubutton。布局中的普通按钮。

2 个答案:

答案 0 :(得分:1)

在onCreateOptionsMenu:

SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("some text");

smenu=menu.add("smenu").setVisible(false).setActionView(searchView);
smenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

然后在onClickListener:

smenu.expandActionView();

smenu.collapseActionView();

答案 1 :(得分:0)

这应该有效

private ActionBar bar;

//....

SearchView searchView = new SearchView(bar.getThemedContext());
searchView.setQueryHint("some text");
searchView.setIconified(false);
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
    // revert to normal menu
    bar.setDisplayOptions(your_opions);
    invalidateOptionsMenu();
});

//...

@Override
public void onClick(View v) {
    // show custom view
    bar.setDisplayOptions(your_opions | ActionBar.DISPLAY_SHOW_CUSTOM);
    bar.setCustomView(searchView);

}