MaterialDrawer使用指南

MaterialDrawer传送门
一个MaterialDrawer就可以将toolbar+drawlayout+ActionBarDrawerToggle结合起来
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 name="material_drawer_background">@color/md_white_1000</color>
    <color name="material_drawer_primary_text">@color/md_grey_800</color>
    <color name="material_drawer_secondary_text">@color/md_grey_800</color>
    <color name="material_drawer_hint_text">@color/material_drawer_dark_hint_text</color>
    <color name="material_drawer_divider">@color/md_grey_600</color>
    <color name="material_drawer_selected">@color/abl_blue</color>
    <color name="material_drawer_selected_text">@color/md_white_1000</color>
    <color name="material_drawer_header_selection_text">@color/material_drawer_dark_primary_text
    </color>
<dimen name="material_drawer_item_primary_text">18sp</dimen>
    <dimen name="material_drawer_width">240dp</dimen>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值