很早之前就像写个android的侧滑菜单加入到项目中了,但是很多给的都是密密麻麻的代码,今天看到了简单快速的实现方法:通过NavigationView+DrawerLayout来实现侧滑菜单的功能。Google I/O 2015 给大家带来了Android Design Support Library,对于希望做md风格的app的来说,简直是天大的喜讯.NavigationView是是Google在5.0之后推出了NavitationView,就是我们左边滑出来的那个菜单。这个菜单整体上分为两部分,上面一部分叫做HeaderLayout,下面的那些点击项都是menu菜单项,这样的效果如果我们要自己写肯定也能写出来,但比较复杂,没有必要,既然Google提供了这个控件,那我们当然选择简单的实现方式。
我们首先看一下这个最终的实现效果吧:
目前只是做了个简单的效果,后面可以应用到不同的项目中,根据具体情况增加一些其他view控件。
马上来看看是具体如何实现的吧:
1.首先要在project structure的dependence中导入v4.design包,导入后可以在build.grade下看到如下信息:
compile 'com.android.support:design:25.2.0'
2.使用NavigationView
首先在主布局文件中使用DrawerLayout作为外包装,里面包含一个主页面和侧滑的菜单页面,而侧滑菜单页面用NavigationView实现:
activity_main.layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_na"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.ui.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue_700"
android:gravity="center_v