Android的 - 开抽屉式导航栏点击应用程序图标?

问题描述:

我在我的应用程序(导航,你从屏幕左侧的滑开的类型)使用的导航抽屉。现在,通常你也可以打开它通过点击应用程序图标,当我看着它,我发现了一大堆code添加到您的活动。而只是一个简单的按钮。我想那不是正是我期待的?如果它真的需要这么多$ C $下一个按钮,是什么使code更清晰的最佳方法?谢谢,对不起,是这样的一个机器人福利局。

I use a navigation drawer in my app (the type of navigation that you open by sliding from the left side of the screen). Now, usually you can also open it by tapping the app icon, and when I looked it up, I found a whole bunch of code to add to your activity. And just for a simple button. I suppose thats not exactly what I am looking for? And if it really needs to be so much code for a single button, what is the best way to make the code more clear? Thank you, and sorry for being such an android newb.

我就遇到了这个问题还,假设你已经有一个ActionBarDrawerToggle为@Kernald建议,您还需要以下内容添加到您的活动:

I ran into this issue also, assuming you already have an ActionBarDrawerToggle as @Kernald suggested, you need to add the following also to your Activity:

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

这让我们拨动手柄上的动作条图标按钮preSS,造成抽屉滑出。

This let's the toggle handle the icon button press on the ActionBar, causing the Drawer to slide out.