參考博客:
NavigationView簡介:
Android Design Support Library 給開發(fā)者帶來了一些重要的 Material Design 組件,并且向下兼容到 Android 2.1呀舔, Navigation View 就是其中之一弥虐,可用于方便地創(chuàng)建導(dǎo)航抽屜扩灯。
NavigationView在很多App上都已經(jīng)可以看到其效果圖,國內(nèi)的比如知乎(側(cè)拉菜單滑出來的那一部分屬于NavigationView)霜瘪,還有Google自己的那些app珠插,也基本全都采用了NavigationView。
實(shí)現(xiàn)效果
<p></p>
使用流程
-
NavigationView要放在一個DrawerLayout中颖对,現(xiàn)在給出我的activity_main.xml捻撑,LinearLayout就是一打開app顯示出的布局,這里我是接著Android框架之路——RecyclerView的demo繼續(xù)使用的缤底,所以里面放了一個RecyclerView顾患,同時也要加上ToolBar。其中android:layout_gravity="left"是讓NavigationView收進(jìn)左側(cè);
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.ping.recyclerview.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:navigationIcon="@drawable/navigation" app:title="Navigation" app:titleTextColor="#fff"> </android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/navigationView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" app:headerLayout="@layout/navigation_header" app:menu="@menu/na_menu"> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
-
在NavigationView中个唧,app:headerLayout="@layout/navigation_header"江解、app:menu="@menu/na_menu"的navigation_header.xml和na_menu.xml如下,na_menu.xml中g(shù)roup與group之間會有橫線。這里給大家分享一個網(wǎng)站https://materialdesignicons.com/徙歼,里面的圖標(biāo)可以右鍵選擇View Vector Drawable方式膘流,然后再資源文件夾下新建drawable用就好了,非常好用鲁沥;
navigation_header.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:background="@drawable/weixin" android:layout_height="200dp"> <ImageView android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:id="@+id/iv_header" android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@mipmap/ic_launcher"/> <TextView android:layout_marginTop="90dp" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Navigation使用" android:textColor="#000" android:textSize="25dp"/> </LinearLayout>
na_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/group1" android:checkableBehavior="single"> <item android:id="@+id/favorite" android:icon="@drawable/favourite" android:title="收藏"/> <item android:id="@+id/wallet" android:icon="@drawable/wallet" android:title="錢包"/> <item android:id="@+id/photo" android:icon="@drawable/photo" android:title="相冊"/> <item android:id="@+id/file" android:icon="@drawable/file" android:title="文件"/> </group> <group android:id="@+id/group2" android:checkableBehavior="single"> <item android:id="@+id/about" android:icon="@drawable/about" android:title="關(guān)于"/> <item android:id="@+id/share" android:icon="@drawable/share" android:title="分享"/> <item android:id="@+id/setting" android:icon="@drawable/setting" android:title="設(shè)置"/> </group> </menu>
-
在MainActivity.java中呼股,需要去實(shí)現(xiàn)功能;
-
點(diǎn)擊頭部事件画恰;
mNavigationView = (NavigationView) findViewById(R.id.navigationView); mNavigationView.getHeaderView(0).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("mNavigationView", "head is clicked!"); } });
-
設(shè)置條目點(diǎn)擊事件彭谁;
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ case R.id.favorite: Log.i("mNavigationView", "收藏 is clicked!"); break; case R.id.wallet: Log.i("mNavigationView", "錢包 is clicked!"); break; case R.id.photo: Log.i("mNavigationView", "相冊 is clicked!"); break; case R.id.file: Log.i("mNavigationView", "文件 is clicked!"); break; case R.id.about: Log.i("mNavigationView", "關(guān)于 is clicked!"); break; case R.id.share: Log.i("mNavigationView", "分享 is clicked!"); break; case R.id.setting: Log.i("mNavigationView", "設(shè)置 is clicked!"); break; } return true; } });
-
設(shè)置toolbar的navigationIcon點(diǎn)擊打開側(cè)邊欄事件;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDrawerLayout.openDrawer(mNavigationView); } });
-