How to open Navigation Drawer with no actionbar, open with just a button

前端 未结 7 1644
悲&欢浪女
悲&欢浪女 2020-12-02 09:37

I have a navigation bar without any actionbar (I don\'t want an actionbar). I\'m trying to make it so that I have a button that can open the navigation drawer.

I kn

7条回答
  •  粉色の甜心
    2020-12-02 09:42

    I have a simpler solution which uses isDrawerOpen() of DrawerLayout.

    The code below closes or opens the navigation drawer based on the drawer's current state (Opened/Closed)

    Button hamMenu = findViewById(R.id.ham_menu);
    hamMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
            // If navigation drawer is not open yet, open it else close it.
            if(!navDrawer.isDrawerOpen(GravityCompat.START)) navDrawer.openDrawer(Gravity.START);
            else navDrawer.closeDrawer(Gravity.END);
        }
    });
    

提交回复
热议问题