如何关闭导航抽屉以使用返回主页图标按钮? [英] How can I dismiss the Navigation Drawer to use the Back-home-icon button?
问题描述
我也在使用带有搜索栏的操作栏,我需要像返回按钮一样使用操作栏图标:
I'm using the action bar with search bar too, and I need to use the ActionBar ico like a Back button:
但我也在使用抽屉式导航...如何关闭/隐藏/禁用导航抽屉菜单以使用后退按钮?
But I'm using the navigation drawer too... How can I dismiss/hide/disable the Navigation Drawer menu to use the back button?
我的动作条码:
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
this.getSupportActionBar().setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.getSupportActionBar().setHomeButtonEnabled(true);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v;
if(!searchView){
v = inflator.inflate(R.layout.action_textview, null);
((TextView) v.findViewById(R.id.titleText)).setText(actionTitle);
menu.getItem(0).setVisible(true);
menu.getItem(2).setVisible(true);
mainMenu = menu;
}else{
v = inflator.inflate(R.layout.action_searchview, null);
actionSearch = (EditText) v.findViewById(R.id.searchText);
actionSearch.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_GO:
case 0:
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(actionSearch.getWindowToken(), 0);
String temp = actionSearch.getText().toString();
searchFor(temp);
break;
}
return true;
}
});
actionSearch.requestFocus();
menu.getItem(0).setVisible(false);
menu.getItem(2).setVisible(false);
}
this.getSupportActionBar().setCustomView(v);
return super.onCreateOptionsMenu(menu);
}
代码帮助:
- My Title 是一个只有 TextView 的自定义视图,用于自定义字体颜色和大小;(action_textview)
- 我的 SearchBar 使用只有一个 EditText 的自定义视图;(action_searchview)
推荐答案
从ActionBarDrawerToggle
可以使用setDrawerIndicatorEnabled(Boolean enable)
方法.或者,您可以设置 ActionBar
显示选项.特别是标志 DISPLAY_HOME_AS_UP
.链接到文档
From ActionBarDrawerToggle
you can use the method setDrawerIndicatorEnabled(Boolean enable)
. Alternatively, you can set the ActionBar
Display options. Specifically the flag DISPLAY_HOME_AS_UP
. Link to docs
然后像往常一样处理点击事件.
Then handle the click event as usual.
这篇关于如何关闭导航抽屉以使用返回主页图标按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!