当前位置: 首页 > 工具软件 > Drawer > 使用案例 >

Navigation Drawer从右侧划出

戴原
2023-12-01

今天研究了一下NavagationDrawer,看了看Android的demo,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选择的函数

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item != null && item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                mDrawerLayout.closeDrawer(Gravity.RIGHT);
            } else {
                mDrawerLayout.openDrawer(Gravity.RIGHT);
            }
        }
        return false;
    }
};

要点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);
    }

这样就可以实现从右侧弹出NavagationDrawer了

 类似资料: