Clicking hamburger icon on Toolbar does not open Navigation Drawer

后端 未结 13 2786
太阳男子
太阳男子 2020-12-14 00:08

I have a simple android.support.v7.widget.Toolbar and all I am trying to do is to open a NavigationDrawer by pressing the \"hamburger\" icon in the top left cor

相关标签:
13条回答
  • 2020-12-14 00:48

    In your ActivityMain.xml, the toolbar is outside of the DrawerLayout. That's the problem. If you want Toolbar to interact with DrawLayout, Toolbar needs to be a child of DrawerLayout.

    To fix the problem, make DrawerLayout the root of your activity. Here's the documentation. The relevant quote is:

    To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.

    So basically, structure your ActivityMain.xml to be something like this:

    <android.support.v4.widget.DrawerLayout ...>
    
        <RelativeLayout ...>
    
            <android.support.v7.widget.Toolbar .../>
    
            <!-- Your other content goes here -->
    
        </RelativeLayout>
    
        <android.support.design.widget.NavigationView .../>
    
    </android.support.v4.widget.DrawerLayout>
    

    That should take care of the problem.

    0 讨论(0)
提交回复
热议问题