我宣布了SearchView:
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("some text");
我的目标是仅在点击按钮后将此搜索视图添加到操作栏。它不是一个menubutton。布局中的普通按钮。
答案 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);
}