LoginSignup
4
4

More than 5 years have passed since last update.

左右から開くNavigationDrawerの実装

Posted at

※Googleのデザインガイドラインに準拠していない実装です。

通常のDrawerの実装と同じくメインコンテンツの領域として FrameLayout を親レイアウトとし、左ドロワーレイアウトに android:layout_gravity="left 、右ドロワーレイアウトに android:layout_gravity="right を指定することで実現できます。

Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/layout_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- コンテンツ領域 -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
            <!-- ドロワー左 -->
            <RelativeLayout
                android:id="@+id/drawer_left"
                android:layout_width="304dip"
                android:layout_height="match_parent"
                android:layout_gravity="left">
            </RelativeLayout>
            <!-- ドロワー左 -->
            <RelativeLayout
                android:id="@+id/drawer_right"
                android:layout_width="304dip"
                android:layout_height="match_parent"
                android:layout_gravity="right">
            </RelativeLayout>
        </DrawerLayout>
    </FrameLayout>
</RelativeLayout>

あとは普通に下記のように各ドロワーを制御できます。

Activity.java
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.layout_drawer);
RelativeLayout drawerLeft = (RelativeLayout)findViewById(R.id.drawer_left);
RelativeLayout drawerRight = (RelativeLayout)findViewById(R.id.drawer_right);

// 制御その1
drawerLayout.closeDrawer(Gravity.LEFT);
drawerLayout.openDrawer(Gravity.RIGHT);

// 制御その2
drawerLayout.openDrawer(drawerLeft);
drawerLayout.closeDrawer(drawerRight);
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4