0

如果我使用导航视图,我需要在我的应用栏右侧有一个 actionbardrawertoggle。

一、main中的导航视图->

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

如果我在工具栏(或应用程序栏)中滑动或单击抽屉切换,我需要在右侧使用它。但是抽屉开关没有显示在右侧,因为在左侧。如果我点击按钮,我会得到这个异常

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

这个例外自然是因为我的布局在右/端。所以,我的问题是如何将这个按钮对齐在右侧?或者我怎样才能用右键解决这个异常?

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
            syncState();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
            syncState();
        }
    };
    drawer.setDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    //add this line to display menu1 when the activity is loaded
    int firstMenu = R.id.nav_menu1;
    displaySelectedScreen(firstMenu);
    navigationView.setCheckedItem(firstMenu);
}

好奇的是,从右向左滑动正在工作,显示导航视图:)(但按钮不起作用)

你能帮助我吗 ?:)

(对不起我的英语不好!)

4

0 回答 0