Navigation Drawer从右侧划出
Navigation Drawer从右侧划出
AndroidStudio自带模板中的demo,NavagationDrawer是从左往右出来的,用过Share微博第三方客户端,ta把Drawer设置成从右往左。
在stackoverflow上找到的答案。
链接http://stackoverflow.com/questions/18547277/how-to-set-navigation-drawer-to-be-opened-from-right-to-left
1.drawer在main.xml中要设置android:layout_gravity="right",
Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)
To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
2.默认是从左向右划出的,如果希望从右向左,需要修改ActionBarDrawerToggle中option选择的函数
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
// this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
3.在Activity的onOptionsItemSelected中添加mDrawerToggle的调用
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
4.已经实现从右侧弹出NavagationDrawer了。但是点击menu三横按钮,页面崩溃了。
The error:IllegalArgumentException: No drawer view found with gravity LEFT
解决办法:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} else {
drawer.openDrawer(Gravity.RIGHT);
}
}
});