Android冒险之旅-26-DrawerLayout的简单使用

目录

效果图

布局

 Activity

END


效果图

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    tools:context=".MainActivity">

    <!-- 内容区 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="内容区"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/btn_open_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开左边"/>

        <Button
            android:id="@+id/btn_open_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开右边"/>

    </LinearLayout>

    <!-- 左边菜单 -->
    <LinearLayout
        android:id="@+id/menu_view"
        android:layout_width="260dp"
        android:orientation="vertical"
        android:background="@color/white"
        android:gravity="center"
        android:layout_height="match_parent"
        android:layout_gravity="start">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="左边菜单"
            android:textColor="@color/myorange"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/btn_close_left"
            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="end"
        android:background="@color/white"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="右边菜单"
            android:textColor="@color/myorange"
            android:textSize="20sp"
            android:textStyle="bold"/>

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

    </LinearLayout>


</androidx.drawerlayout.widget.DrawerLayout>

 Activity

            case R.id.btn_open_left:
                //打开左边
                drawerLayout.openDrawer(Gravity.LEFT);
                break;
            case R.id.btn_open_right:
                //打开右边
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
            case R.id.btn_close_left:
                //关闭左边
                drawerLayout.closeDrawer(Gravity.LEFT);
                break;
            case R.id.btn_close_right:
                //关闭所有
                drawerLayout.closeDrawers();
                break;

END

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值