普通的菜单都是放在toolbar上,但是那样会让我们的 app看起来很难看,而所谓的滑动菜单,就是将一些菜单选项隐藏起来,而不是放置在主屏幕上,然后用户就可以通过滑动的方式将菜单显示出来,这种方式既节省屏幕空间,又实现了良好的动画效果。
这个功能看起来比较难实现,如果要自己实现的话确实是这样,不过google早就想到这一点了,Google提供了一个DrawerLayout插件,借助这个控件来实现滑动菜单就很方便了。
DrawerLayout,顾名思义,他是一个布局,该布局中允许开发者放入两个直接子控件,第一个子控件是主屏幕中显示的内容,第二个控件是滑动菜单中显示的内容,如下:
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/drawer”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<FrameLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<android.support.design.widget.NavigationView
android:id=“@+id/nav_view”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_gravity=“start”
android:background=“@drawable/lanhua”
app:headerLayout=“@layout/nav_header”
app:menu=“@menu/nav_menu”>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
其中的FrameLayout布局就是用来放主屏幕的配置文件的,剩下的这个布局就是显示在滑动菜单的内容了,这边可以写任意的布局文件,上面的这个NavigationView也是谷歌官方的Design Support库中提供的一个控件,具体用法就上面这样