本文地址:http://blog.csdn.net/u011181236/article/details/51985242
1.布局要注意的就是
android:layout_gravity=”right”
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
2.java代码
这个是设置toolbar里左上角menu按钮的点击事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!drawer.isDrawerOpen(setDrawerGravity())) {
drawer.openDrawer(setDrawerGravity());
}
}
});
3.这里只有4个属性可用
Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right.GravityCompat.START or GravityCompat.END may also be used.
private int setDrawerGravity() {
return Gravity.RIGHT;
}
4.返回键处理
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(setDrawerGravity())) {
drawer.closeDrawer(setDrawerGravity());
} else {
super.onBackPressed();
}
}
5.NavigationView 的item点击处理
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(setDrawerGravity());
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
}
return true;
}
});