DrawerLayout

447 阅读1分钟

只是无聊写一下,基本用不到

DrawerLayout里嵌套两个布局,一个主布局,另一个抽屉布局。

先写主布局,再写抽屉布局,且需要通过layout_gravity指定抽屉布局是从左边还是右边拉出来

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!--主界面-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="展示侧滑"/>

    </LinearLayout>
    
    <!--侧滑界面-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@color/colorAccent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭侧滑"/>

    </LinearLayout>

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

//展示抽屉布局或者隐藏抽屉布局

        btn.setOnClickListener{
            drawer_layout.openDrawer(Gravity.LEFT)
        }

//        点击侧滑界面中的按钮缩回侧滑界面
        btn2.setOnClickListener{
            drawer_layout.closeDrawer(Gravity.LEFT)
        }