1.添加依赖
compile(‘com.mikepenz:materialdrawer:5.1.6@aar’) {
transitive = true
}
2.直接在代码中新建
private Drawer mainDrawer = null;
private void initSlidingDrawer(Bundle savedInstanceState) {
// Handle Toolbar
mainDrawer = new DrawerBuilder()
.withActivity(this)
//跟drawerlayout用法相同,用这个drawer替换覆盖掉原来drawerlayout的位置
.withRootView(R.id.drawer_container)
.withHeader(R.layout.view_drawer_header)
.withHeaderDivider(false)
.withSavedInstance(savedInstanceState)
//与toolbar关联起来
.withToolbar(mToolbar)
//启用toolbar的ActionBarDrawerToggle动画
.withActionBarDrawerToggleAnimated(true)
.withDisplayBelowStatusBar(false)
.withTranslucentStatusBar(false)
.withDrawerLayout(R.layout.material_drawer)
.addDrawerItems(
new PrimaryDrawerItem().withName("需求信息").withIcon(R.mipmap.ic_assignment_turned_in_black_48dp).withIdentifier(1),
new PrimaryDrawerItem().withName("我的商品").withIcon(R.mipmap.ic_local_grocery_store_black_48dp).withIdentifier(2),
new PrimaryDrawerItem().withName("我的消息").withIcon(R.mipmap.ic_chat_bubble_outline_black_48dp).withIdentifier(3),
new PrimaryDrawerItem().withName("设置").withIcon(R.mipmap.ic_build_black_48dp).withIdentifier(4)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem != null) {
switch (drawerItem.getIdentifier()) {
case 1:
setToolBarTitle("需求信息");
fragUtils.showFragment(Tag_DemandProductListFrag);
break;
case 2:
setToolBarTitle("我的商品");
fragUtils.showFragment(Tag_SupplyProductListFrag);
break;
case 3:
setToolBarTitle("我的消息");
fragUtils.showFragment(Tag_MyMessageListFrag);
break;
case 4:
setToolBarTitle("设置");
fragUtils.showFragment(Tag_SettingFrag);
break;
default:
break;
}
}
return false;
}
}).build();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState = mainDrawer.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
3.在color.xml、dimen.xml和style.xml中对MaterialDrawer的各个元素进行自定义设置
@color/md_white_1000
@color/md_grey_800
@color/md_grey_800
@color/material_drawer_dark_hint_text
@color/md_grey_600
@color/abl_blue
@color/md_white_1000
@color/material_drawer_dark_primary_text
4.dimen 中的一些配置
18sp
240dp