MaterialDrawer使用指南

2018-01-03  本文已影响0人  丶Mars绝版

https://github.com/mikepenz/MaterialDrawer //项目地址

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>

4.dimen 中的一些配置

    <dimen name="material_drawer_item_primary_text">18sp</dimen>
    <dimen name="material_drawer_width">240dp</dimen>
上一篇下一篇

猜你喜欢

热点阅读