android 双抽屉:右抽屉切换

android double drawer: right drawer toggle

我看过很多关于双抽屉的帖子,但是 none 解决了右抽屉切换问题。

Android 双抽屉布局:

<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="start">

<!-- content here -->

<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="left"/>


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer2"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"/>

</android.support.v4.widget.DrawerLayout>

当时的主要活动

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();

左侧抽屉只有一个切换图标。右边的抽屉没有。 ActionBarDrawerToggle 没有指定哪个抽屉。

我认为您可以自行解决以显示右侧抽屉的图标,将项目放入 activity 菜单中以打开右侧抽屉。

将此放入您的 main_menu.xml

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

    <item
        android:id="@+id/action_open_right_drawer"
        android:icon="@mipmap/ic_ab_right_drawer_icon"
        android:title="rightDrawer"
        app:showAsAction="always" />
</menu>

并将其放入您的 mainActivity

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.action_open_right_drawer)
        drawerLayout.openDrawer(GravityCompat.END);

    return super.onOptionsItemSelected(item);
} 

我确定这不是最好的解决方案,但效果很好