首先咱们在studio中建立工程的时候,选择activity时选择DrawerActivity。这样编辑器会自动给你建立完成一套demo。android
首先,解析对应的activity:git
1, activity继承自AppCompatActivity实现NavgitionView.OnNevigationItemSelectedLisenter(侧滑菜单部分的item项的选中监听)。app
2, <?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main_tab"
app:menu="@menu/activity_main_tab_drawer" />
</android.support.v4.widget.DrawerLayout>
这是activity对应的主布局。编辑器
NavigationView对应如图所示:模块化
NavigationView中包含两个部分:上面的绿色的部分,和下面的菜单项是分开的布局
菜单项:spa
app:headerLayout="@layout/nav_header_main_tab"
app:menu="@menu/activity_main_tab_drawer"
让咱们仔细看NavigationView的两个属性:一个是headLayout,一个是menu。很显然:headLayout对应绿色部分,menu对应下面的菜单。code
而后咱们在看:在NavegationView除外的部分:orm
<include
layout="@layout/app_bar_main_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" />
include里面包含的是:一个layout,咱们在看layout当中是指向谁:xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.test.sampletest.MainTabActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main_tab" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
点了进去咱们能够看到:截图以下:
如此能够看到:这里是如NavigationView以外的部分。
这里的布局其实就是除了ToolBar和下面的邮件图标意外的部分。这里就是本身能够自定义其余控件的部分。
写了这么多最后说明白点:DrawerLayout就至关于一个侧滑菜单,是官方一个模仿以后封装模块化的一套控件而已。一个主布局:DrawerLayout,他包含了两个部分:NavigationView,include一个布局。
NavigationView又分为两个部分:上部headLayout,下部menu。
Include的布局:官方的控件android.support.design.widget.CoordinatorLayout
子布局:首先一个ToolBar,而后include一个布局(自定义样式)。下部是一个邮件图标。
上面写的这些都是按照官方的步骤来写。加入本身的深刻理解后可得出这样的结论:
主布局最外层:DrawerLayout。而后咱们能够吧NavigationView换成listView照样是能够的。而后在include一个布局,里面放一个Toolsbar如此而已。