我正在尝试从右到左打开导航视图,但我失败了。我看过很多问题,他们说的话似乎不完整。
以下是我根据这些问题所做的事情:
MainActivity
)activity_main.xml
并为layout_gravity="right"
NavigationView
MainActivity.java
中将所有GravityCompat.START
设置为Gravity.RIGHT
但是当我运行应用程序并点击操作栏内的抽屉切换时,我收到此错误:
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)
请帮忙。
答案 0 :(得分:0)
实际上问题出在 ActionBarDrawerToggle 构造函数中 除了你提到的上述变化,你还应该尝试这个
第1步:从ActionBarDrawerToggle构造函数中删除工具栏参数。现在应该是这样的
toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
第2步: 自定义选项菜单以将其用于导航抽屉
@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>