在我的演示文稿中将搜索视图和子菜单放在我的操作栏上,如下所示: @覆盖 public boolean onPrepareOptionsMenu(菜单菜单){
SearchView searchView = new SearchView(getSupportActionBar()
.getThemedContext());
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if(currentPosition == 0) {
System.out.println("Now search index 0 data...");
}else if(currentPosition == 1) {
System.out.println("Now search index 1 data...");
}
return false;
}
});
menu.add(Menu.NONE, 0, 0, "")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
SubMenu sub = menu.addSubMenu(Menu.NONE, 1, 1, "").setIcon(
R.drawable.abs__ic_menu_moreoverflow_holo_dark);
if(currentPosition == 0) {
sub.add(0, R.style.Theme_Sherlock, 0, "Mail");
sub.add(0, R.style.Theme_Sherlock_Light, 0, "IM");
}else if(currentPosition == 1) {
sub.add(0, R.style.Theme_Sherlock, 0, "City");
sub.add(0, R.style.Theme_Sherlock_Light, 0, "Relocate");
}
sub.getItem().setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onPrepareOptionsMenu(menu);
}
使用HoloEveryWhere单击ActionBar上的SearchView图标时,如何隐藏SubMenu?
答案 0 :(得分:1)
像这样:
menu.add(...)
...
.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
sub.getItem().setVisible(false);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
sub.getItem().setVisible(true);
return true;
}
})
...
虽然我想知道你为什么要创建自己的溢出菜单作为子菜单。为什么不使用默认的溢出功能?