1 开始使用
compile 'com.android.support:design:25.3.1'
2 滑动菜单不覆盖Toolbar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/id_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 内容布局 -->
<FrameLayout
android:id="@+id/id_framelayout_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:padding="16dp"
android:text="test"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<!-- 侧滑布局 -->
<android.support.design.widget.NavigationView
android:id="@+id/id_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/layout_navigation_header_layout"
app:menu="@menu/id_menu_navigation_view"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
- layout_navigation_header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="8dp"
android:background="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:text="十年生死两茫茫"
android:textColor="@android:color/white"
android:textSize="16sp"/>
</RelativeLayout>
- id_menu_navigation_view.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import"/>
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery"/>
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow"/>
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share"/>
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send"/>
</menu>
</item>
</menu>
3 配合Toolbar左侧按钮
- 设置
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.id_drawer_layout);
//左侧按钮 旋转
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
toolbar,
R.string.openDrawerContentDesc, R.string.closeDrawerContentDesc);
drawer.addDrawerListener(toggle);
toggle.syncState();//同步状态
- 不设置
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
R.string.openDrawerContentDesc, R.string.closeDrawerContentDesc);
4 滑动菜单覆盖Toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/id_drawer_layout"
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:openDrawer="start">
<LinearLayout
android:id="@+id/id_ll_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
<TextView
android:padding="16dp"
android:text="test"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:weight="1"
/>
</LinearLayout>
<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_drawer_layout__one"
app:menu="@menu/activity_drawer_layout__one_drawer"/>
</android.support.v4.widget.DrawerLayout>
5 监听菜单点击
nv_menu_left.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_item1:
mToolBar.setTitle("选项一");
break;
case R.id.navigation_item2:
mToolBar.setTitle("选项二");
break;
case R.id.navigation_item3:
mToolBar.setTitle("选项三");
break;
case R.id.navigation_sub_item1:
mToolBar.setTitle("简介");
break;
case R.id.navigation_sub_item2:
mToolBar.setTitle("详情");
break;
case R.id.navigation_sub_item3:
mToolBar.setTitle("更多");
break;
}
//选中的条件是该menu的checkable为true
item.setCheckable(true);
//将选中设为文字选中的状态
//item.setChecked(true);
//
//id_navigation_view.setCheckedItem(item.getItemId());
//关闭抽屉
mDrawerLayout.closeDrawers();
return true;
}
});
6 菜单样式相关
- 让菜单图片显示原来的颜色
app:itemIconTint=""
//或者
mNavigationView.setItemIconTintList(null);
- 菜单过多,隐藏滚动条
NavigationMenuView menuView = (NavigationMenuView) mNavigationView.getChildAt(0);
menuView.setVerticalScrollBarEnabled(false);
- 样式自定义
菜单item背景
app:itemBackground
菜单item文字颜色
app:itemTextColor
文字样式
app:itemTextAppearance
- 默认选中菜单
menu xml 设置
android:checked="true"
- 默认选中菜单(带背景)
方法1
menu xml 设置
android:checkable="true"
代码中 设置
//设置默认选中 选中的条件是该menu的checkable为true
id_navigation_view.setCheckedItem(R.id.id_menu_item_patient_list);
方法2
代码中 设置
//设置默认选中
if (id_navigation_view.getMenu()!=null&&id_navigation_view.getMenu().size()>0){
mMenuItem=id_navigation_view.getMenu().getItem(0);
mMenuItem.setCheckable(true);
//选中的条件是该menu的checkable为true
id_navigation_view.setCheckedItem(mMenuItem.getItemId());
}
- 动态隐藏菜单
NavigationView.getMenu().findItem(R.id.item_1);
item.setVisible(false);//false即隐藏,true即显示
7 获取head的view
headerView = mNavigationView.getHeaderView(0);
8 监听侧滑
- onDrawerSlide方法中的slideOffset,则对应于DrawerLayout展开的偏移值,全部展开时为1,全部收起时为0,onDrawerStateChanged对应于所处的状态:STATE_IDLE/STATE_DRAGGING/STATE_SETTLING
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Log.d("mDrawerLayout", "onDrawerSlide, slideOffset=" + slideOffset);
}
@Override
public void onDrawerOpened(View drawerView) {
Log.d("mDrawerLayout", "onDrawerOpened");
}
@Override
public void onDrawerClosed(View drawerView) {
Log.d("mDrawerLayout", "onDrawerClosed");
}
@Override
public void onDrawerStateChanged(int newState) {
Log.d("mDrawerLayout", "onDrawerStateChanged, state=" + newState);
}
});
9 参考文档
- Android Design Support Library系列之二:NavigationView的使用 //www.greatytc.com/p/ddd23992b3ed
- Material Design 控件知识梳理(5) - DrawerLayout && NavigationView //www.greatytc.com/p/d70cfd724c7f