从blueprint可以看出,NavigationView在布局的外侧
DrawerLayout布局中需要有NavigationView,不然会报错
使用方法
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//设置toolbar
setSupportActionBar(toolbar);
//设置左上角图标是否可点击
getSupportActionBar().setHomeButtonEnabled(true);
//左上角加上一个返回图标
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//初始化ActionBarDrawerToggle(ActionBarDrawerToggle就是一个开关一样用来打开或者关闭drawer)
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close){
/*
* 抽屉菜单打开监听
* */
@Override
public void onDrawerOpened(View drawerView) {
Toast.makeText(StandardContentActivity.this,"菜单打开了",Toast.LENGTH_SHORT).show();
super.onDrawerOpened(drawerView);
}
/*
* 抽屉菜单关闭监听
* */
@Override
public void onDrawerClosed(View drawerView) {
Toast.makeText(StandardContentActivity.this,"菜单关闭了",Toast.LENGTH_SHORT).show();
super.onDrawerClosed(drawerView);
}
};
drawerToggle.syncState();
//设置DrawerLayout的抽屉开关监听
drawerLayout.setDrawerListener(drawerToggle);
手动打开策划菜单的代码
drawerLayout.openDrawer(Gravity.END);
网友评论