NavigationView是什么?
这是Google在Android5.0之后推出的一个控件,兼容到Android2.1,代替之前自己做抽屉菜单,简单方便。NavigationView整体分为上下两个部分,上部分叫做HeaderLayout,下面的点击项都是menu。
NavigationView的使用方法
1、写布局文件
整体布局
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmls:app="http://schemas.android.com/apk/res-auto"
xmls:tools="http://schemas.android.com/tools"
android:id="@_id/item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/weather_layout"
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"
app:menu="@menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
代码解释:最外层是个DrawerLayout,
headerLayout
看一下headerLayout(app:headerLayout=”@layout/nav_header_main”)的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#81c0c0"
android:gravity="bottom"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
andro