android - 通过操作栏标题切换抽屉导航

标签 android gmail android-actionbar toggle navigation-drawer

我试图让用户通过点击操作栏标题来打开/关闭我的应用程序中的抽屉导航(这是当前 Android Gmail 应用程序的设置方式)。目前,用户可以通过点击应用程序/抽屉图标或通过左右滑动将其滑入来切换抽屉。但是,操作栏标题本身是不可点击的。根据developer docs , 当我们使用 NAVIGATION_MODE_STANDARD 但出于某种原因,我无法让标题以这种方式运行。

我相信 Navigation Drawer 本身没问题,但这是我设置 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);

}

如有任何建议,我们将不胜感激!

最佳答案

如果您希望通过点击 ActionBar 的图标/标题打开抽屉,我建议您使用支持库(android.support.v4.app. ActionBarDrawerToggle)

引用: https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

使用示例:
https://developer.android.com/training/implementing-navigation/nav-drawer.html

当在 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);
}

关于android - 通过操作栏标题切换抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18661222/

相关文章:

java - 如何使用自定义 webview 和覆盖功能?

android - Android Gmail 客户端不支持 Html 行(水平规则)

python - 错误 252 : b"2. 1.5 发送一些邮件,我会尽力 f18sm1267047wiv.14"

android - 首选项菜单中 Android 向上/向后导航的动态父 Activity

android - 如何在 Android 中截断具有淡入淡出效果的文本?

android - Android 是否将从 HTTP 下载的图像保存在缓存中?

android - 带有 Visual Studio 的 Android 模拟器上的奇怪解决方案

ios - 通过 Gmail API 获取未读邮件数

java - 添加 TabListener 时出现 NullPointerException

android - 带有 ActionBarSherlock 的原生 ActionBar selectableItemBackground