How to configure NavigationView to open from Right

2019-07-28 05:56发布

I'm trying to make navigation view open from Right-to-Left, but I fail. I've seen many questions, and what they say seems to be incomplete.

Here's what I've done based on these questions:

  • Creating a new project in Android Studio
  • Selecting the Navigation Drawer Activity template as the default template for the main activity (naming it MainActivity)
  • Going to activity_main.xml and setting layout_gravity="right" for NavigationView
  • Then in the MainActivity.java set all GravityCompat.STARTs to Gravity.RIGHT

But when I run the app, and click on the drawer toggle inside the action bar, I get this error:

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
    at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1651)
    at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1637)
    at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:293)
    at android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:202)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Please help.

1条回答
做个烂人
2楼-- · 2019-07-28 06:41

Actually the problem is with the ActionBarDrawerToggle constructor Apart from above changes you have mentioned you should also try this

Step 1: Remove the toolbar parameter from the ActionBarDrawerToggle constructor.It should be like this now

toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);

Step 2: customize your options menu to use it for navigation drawer

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem itemCart = menu.findItem(R.id.action_burger);
    LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_burger:
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if(drawer.isDrawerOpen(GravityCompat.END)) {
                drawer.closeDrawer(GravityCompat.END);
            }else
                drawer.openDrawer(GravityCompat.END);
            break;

    }
    return super.onOptionsItemSelected(item);
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.mobikul.MainActivity" >
<item
    android:id="@+id/action_burger"
    android:icon="@drawable/ic_menu_notification"
    android:title="Settings"
    app:showAsAction="always" />
</menu>

ic_menu_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@drawable/burger_icon"
    android:gravity="center" />
<item
    android:id="@+id/ic_badge"
    android:drawable="@drawable/burger_icon" />
</layer-list>
查看更多
登录 后发表回答