如何配置NavigationView在Android Studio中从右向左打开? [英] How to configure NavigationView to open from Right-to-Left in Android Studio?

查看:482
本文介绍了如何配置NavigationView在Android Studio中从右向左打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从右到左打开导航视图,但是失败了.我看过很多问题,他们说的话似乎还不完整.

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:

  • 在Android Studio中创建新项目
  • 选择导航抽屉活动模板作为主要活动的默认模板(将其命名为MainActivity)
  • 转到activity_main.xml并为NavigationView设置layout_gravity="right"
  • 然后在MainActivity.java中将所有GravityCompat.START设置为Gravity.RIGHT
  • 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)

请帮助.

推荐答案

实际上问题在于 ActionBarDrawerToggle 构造函数 除了您提到的上述更改之外,您还应该尝试此操作

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

步骤1:从ActionBarDrawerToggle构造函数中删除工具栏参数.现在应该像这样

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);

第2步: 自定义您的选项菜单以将其用于导航抽屉

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>

这篇关于如何配置NavigationView在Android Studio中从右向左打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆