[英]Toggle the Navigation Drawer via the Action Bar title
I'm trying to allow the user to open/close the navigation drawer in my app by tapping the action bar title (this is how the current Android Gmail app is set up). 我试图通过点击操作栏标题(这是当前Android Gmail应用程序的设置方式)允许用户在我的应用中打开/关闭导航抽屉。 At the moment, the user can toggle the drawer by tapping the app/drawer icon or by sliding it in with a left-right swipe. 此时,用户可以通过点击应用程序/抽屉图标或通过左右滑动将其滑入来切换抽屉。 However, the action bar title itself is not clickable. 但是,操作栏标题本身不可单击。 According to the developer docs , clicking the action bar title should "dispatch onOptionsItemSelected
to the host Activity with a MenuItem with item ID android.R.id.home
" when we use NAVIGATION_MODE_STANDARD
but for some reason I can't get the title to behave this way. 根据开发人员文档 ,当我们使用NAVIGATION_MODE_STANDARD
时,单击操作栏标题应该“使用具有项目ID android.R.id.home
的MenuItem将onOptionsItemSelected
调度到主机Activity”但由于某种原因我无法获得行为标题这条路。
I believe the Navigation Drawer itself is fine, but here is how I set up the Action Bar: 我相信导航抽屉本身很好,但这是我如何设置动作栏:
private void configureActionBar(CharSequence mTitle) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.ic_blank);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {
0xFF004700, 0xFF002900
});
gd.setCornerRadius(0f);
actionBar.setBackgroundDrawable(gd);
// set the title of the action bar using the given title
actionBar.setTitle(mTitle);
}
Any suggestions would be greatly appreciated! 任何建议将不胜感激!
If you want the drawer to open by tapping Icon/Title of ActionBar, i suggest you to use the ActionBarDrawerToggle class provided in the support library( android.support.v4.app.ActionBarDrawerToggle ) 如果你想通过点击ActionBar的Icon / Title来打开抽屉,我建议你使用支持库中提供的ActionBarDrawerToggle类( android.support.v4.app.ActionBarDrawerToggle )
Reference: https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html 参考: https : //developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
Example of use: 使用示例:
https://developer.android.com/training/implementing-navigation/nav-drawer.html https://developer.android.com/training/implementing-navigation/nav-drawer.html
The trick comes when capturing the event in onOptionsItemSelected(), that you have to pass it to the ActionBarDrawerToggle, so it can handle the open/close drawer request: 当捕获onOptionsItemSelected()中的事件时,必须将其传递给ActionBarDrawerToggle,以便它可以处理打开/关闭抽屉请求:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
The Icon/Title is shown if application theme attribute is set in AndroidManifest.xml like this: 如果在AndroidManifest.xml中设置应用程序主题属性,则显示图标/标题,如下所示:
<application
android:name=".SampleApp"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
res/values/styles.xml holds the theme declaration res / values / styles.xml包含主题声明
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
It works by using android.support.v7.app.ActionBarDrawerToggle, the support.v4 class is deprecated in the meantime. 它的工作原理是使用android.support.v7.app.ActionBarDrawerToggle,同时不推荐使用support.v4类。 See How to use support-v7-appcompat library 请参见如何使用support-v7-appcompat库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.