MaterialDrawer开源侧滑菜单的使用手册
官方有详细说明,但是我首次查找的时候并没有第一眼就能使用全部功能,而网上也查找了一下,几乎所有的博客都是简简单单的几句代码。。。连句说明都没有,可能是我这小菜鸡理解能力不行,或者大神们认为coding的都是大神。。。
首先也是步骤,官方链接如下:https://github.com/mikepenz/MaterialDrawer
按照官方所说,第一步在gradle中添加
compile('com.mikepenz:materialdrawer:4.4.1@aar') {
transitive = true
}
第二步就是新建我们的侧滑菜单了,这就是有点疑难的地方,我们平时想到的侧滑菜单,或者是support:design包中系统提供的DrawerLayout实现侧滑菜单,他们都是在布局中进行设计的,而MaterialDrawer开源侧滑菜单的话在Activity中添加逻辑代码即可实现,并且如果系统的和开源的同时存在时,开源菜单会覆盖掉系统的,下面就是最简单的新建一个侧滑菜单代码:
new DrawerBuilder()
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog),
new SecondaryDrawerItem().withName(R.string.drawer_item_help).withIcon(FontAwesome.Icon.faw_question).withEnabled(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_bullhorn)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem != null && drawerItem.getIdentifier() == 1) {
startSupportActionMode(new ActionBarCallBack());
findViewById(R.id.action_mode_bar).setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(CompactHeaderDrawerActivity.this, R.attr.colorPrimary, R.color.material_drawer_primary));
}
if (drawerItem instanceof Nameable) {
toolbar.setTitle(((Nameable) drawerItem).getName().getText(CompactHeaderDrawerActivity.this));
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.build();
好吧,一大堆代码贴上,然而并没有什么卵用,关键是所有设置都在逻辑代码里面,下面抽出一个样式来说下:
我们虽然没有布局,但是我们有主题啊,使用主题对侧滑菜单进行样式设计,很人性化吧
<style name="CustomTheme" parent="MaterialDrawerTheme.TranslucentStatus">
<!-- ...and here we setting appcompat’s color theme attrs -->
<item name="colorPrimary">#F1433C</item>
<item name="colorPrimaryDark">#D03033</item>
<item name="colorAccent">#02A8F3</item>
<!-- MaterialDrawer specific values -->
<item name="material_drawer_background">@color/material_drawer_dark_background</item>
<item name="material_drawer_primary_text">@color/material_drawer_dark_primary_text</item>
<item name="material_drawer_secondary_text">@color/material_drawer_dark_secondary_text
</item>
<item name="material_drawer_hint_text">@color/material_drawer_dark_hint_text</item>
<item name="material_drawer_divider">@color/material_drawer_dark_divider</item>
<item name="material_drawer_selected">@color/material_drawer_dark_selected</item>
<item name="material_drawer_selected_text">#F1433C</item>
<item name="material_drawer_header_selection_text">
@color/material_drawer_dark_primary_text
</item>
<!-- CAB :D -->
<item name="windowActionModeOverlay">true</item>
</style>
为你的侧滑菜单所在Activitiy添加如上Style风格之后就有了效果,具体效果自行翻译每个Item的名字!!!
来自蜀汉玫瑰编辑!