如何用Fab打开NavigationDrawer?

时间:2016-10-18 15:33:31

标签: android android-layout android-studio android-fragments

我想点击FAB按钮时打开导航抽屉。

怎么办?

2 个答案:

答案 0 :(得分:3)

您可以查看此DrawerLayout

mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.myFAB); 
myFab.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
       //if you need open the slide:
       mDrawerLayout.openDrawer(Gravity.LEFT);

       //if you need close the slide
       mDrawerLayout.closeDrawer(Gravity.LEFT);  
    } 
});

答案 1 :(得分:1)

mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

并在Fab Onclick中添加以下行

mDrawerLayout.openDrawer(Gravity.LEFT);