Navigation View - Material Design Support Library Tutorial
知識點小摘
- 添加Material Design Support Library 庫的依賴:
compile 'com.android.support:design:22.2.0(或者最新版本) - 添加CircleImage的依賴庫:github地址: circleimageview
dependencies {
...
compile 'de.hdodenhof:circleimageview:2.0.0'
}
- Navigation View主要包含以下兩個重要的組成
- Header View
一般為個人主頁信息帘腹,包含個人頭像贰盗,名稱,Email阳欲,簡介等信息舵盈。
在Layout文件夾下創(chuàng)建Header_view.layout的文件,定義Header的布局 - Menu
包含應(yīng)用的菜單球化,比如知乎的Menu就包含:首頁秽晚,發(fā)現(xiàn),關(guān)注筒愚,收藏赴蝇,圓桌,私信巢掺,切換主題和設(shè)置句伶。
這個應(yīng)該是應(yīng)用層次結(jié)構(gòu)上的一級菜單分類。
在res/下創(chuàng)建menu的目錄陆淀,然后新建drawer.xml 文件用于設(shè)置目錄選項
<code>
....
<item
android:id="@+id/inbox"
android:checked="false"
android:icon="@drawable/ic_inbox_black" //01
android:title="@string/inbox_string" /> //02
....
</code> -
01用于設(shè)置Item的Icon考余,02設(shè)置Item的名稱
Navigation.png
- Navigation View的Header和Menu設(shè)置好后就可以在activity_main.xml文件中使用了:
**app****:headerLayout=****"@layout/header" **//設(shè)置header
app****:menu=****"@menu/drawer"//設(shè)置菜單選項
android****:layout_gravity=****"start"//左對齊 - 此時:Navigation View需要和android.support.v4.widget.DrawerLayout配合使用
- DrawerLayout包含了兩部分:
- DrawerLayout作為根布局包含了兩個子View分別對應(yīng)于Content View和導(dǎo)航欄Navigation View.
<code>
main_activity.xml布局完整代碼如下:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
</code>