ActionBarDrawerToggle右侧在android上导航视图

时间:2017-03-22 09:01:18

标签: java android xml android-navigation-drawer actionbardrawertoggle

如果我使用导航视图,我需要在我的应用栏的右侧进行动作标记。

首先,主要的导航视图 - >

<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" />

如果我在工具栏(或appbar)中的抽屉切换中滑动或点击,我需要在右侧。 但抽屉的开关没有显示在右侧,因为在左侧。 如果我点击按钮,我会得到这个例外

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

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

你能帮帮我吗? :)

(抱歉我的英文不好!)

0 个答案:

没有答案
相关问题