【问题标题】:Toggle the Navigation Drawer via the Action Bar title通过操作栏标题切换导航抽屉
【发布时间】:2013-09-10 18:10:40
【问题描述】:

我试图允许用户通过点击操作栏标题来打开/关闭我的应用中的导航抽屉(这是当前 Android Gmail 应用的设置方式)。目前,用户可以通过点击应用程序/抽屉图标或左右滑动来切换抽屉。但是,操作栏标题本身是不可点击的。根据developer docs,当我们使用NAVIGATION_MODE_STANDARD 时,单击操作栏标题应该“将onOptionsItemSelected 发送到带有项目ID android.R.id.home 的MenuItem 的主机Activity”,但由于某种原因我无法获得标题以这种方式行事。

我相信导航抽屉本身很好,但这是我设置操作栏的方式:

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);

}

任何建议将不胜感激!

【问题讨论】:

    标签: android gmail android-actionbar toggle navigation-drawer


    【解决方案1】:

    如果您希望通过点击 ActionBar 的图标/标题来打开抽屉,我建议您使用支持库中提供的 ActionBarDrawerToggle 类(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);
    }
    

    【讨论】:

    • 感谢您的帖子 - 实际上我已经按照您的描述将事件传递给 mDrawerToggle(这就是为什么点击应用程序图标会按预期切换抽屉),但我无法获得相同的功能从 ActionBar 标题(与图标分开,如在 Gmail 应用程序中)。我想可能可以使用带有 TextView 作为标题的 ActionBar 的自定义视图 - 然后我可以为视图设置一个 onClick 侦听器并调用 openDrawer(),但这似乎不如我希望的解决方案优雅找到。不过谢谢!
    • 嗯,很奇怪,因为我正在做的当前应用程序也适用于标题。事实上,图标+标题就像一个唯一的按钮,因为它突出显示我点击的任何内容。使用来自支持库的 ActionBarDrawerToggle 可能存在问题,而 ActionBar 不是。尝试使用支持库中的 ActionBar?你必须做 getSupportActionBar() 而不是 getActionBar() ...只是猜测
    • 啊,不错。我没有使用 SupportActionBar,所以我一定会尝试一下。谢谢!
    • @DSCroz,如果对您有帮助,请接受正确的答案!谢谢
    【解决方案2】:

    如果在 AndroidManifest.xml 中设置了应用程序主题属性,则会显示图标/标题,如下所示:

        <application
        android:name=".SampleApp"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
    

    res/values/styles.xml 包含主题声明

    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    

    它通过使用 android.support.v7.app.ActionBarDrawerToggle 工作,同时 support.v4 类已被弃用。见How to use support-v7-appcompat library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多