DrawerLayout+NavigationView实现侧边滑出菜单,一篇就够了!

引言

  在Android开发中的侧边滑出菜单栏。DrawerLayout+NavigationView实现侧边滑出菜单,一篇就够了!


效果预览

滑出菜单.gif

用法

第一步:布局文件

(1)主布局:activity_case13.xml

<androidx.constraintlayout.widget.ConstraintLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".show.Case13"
        tools:ignore="MissingConstraints">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/green"
                android:gravity="center"
                android:text="滑出菜单"
                android:textColor="@color/white"
                android:textSize="20sp" />

        <androidx.drawerlayout.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- 内容区 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@color/green"
                    android:gravity="center"
                    android:text="滑出菜单"
                    android:textColor="@color/white"
                    android:textSize="20sp" />

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

            <!-- 左边菜单 -->

            <com.google.android.material.navigation.NavigationView
                android:id="@+id/navigation_view"
                android:layout_width="260dp"
                android:layout_height="match_parent"
                app:itemIconTint="@color/green"
                android:layout_gravity="start"
                app:headerLayout="@layout/drawer_header"
                app:menu="@menu/menu_drawer_left"/>

            <!-- 右边菜单 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="end"
                android:background="@color/black"
                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/white"
                    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>
    </androidx.constraintlayout.widget.ConstraintLayout>

(2)layout下新建:滑出菜单顶部布局draw_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/meizi" />
</LinearLayout>

(3)新建res下的menu文件夹,并新建menu_drawer_left.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/favorite"
        android:icon="@drawable/icon"
        android:title="我的积分" />

    <item
        android:id="@+id/wallet"
        android:icon="@drawable/icon"
        android:title="我的分享" />

    <item
        android:id="@+id/photo"
        android:icon="@drawable/icon"
        android:title="我的收藏" />

    <item
        android:id="@+id/file"
        android:icon="@drawable/icon"
        android:title="TODO" />
    <item
        android:id="@+id/setting"
        android:icon="@drawable/icon"
        android:title="系统设置" />
    <item
        android:id="@+id/about"
        android:icon="@drawable/icon"
        android:title="关于我们" />
    <item
        android:id="@+id/esc"
        android:icon="@drawable/icon"
        android:title="退出登录" />
</menu>

第二步:activity处理点击事件

public class Case13 extends AppCompatActivity {

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case13);
        //初始化
        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        Button btnOpenLeft = findViewById(R.id.btn_open_left);
        Button btnOpenRight = findViewById(R.id.btn_open_right);
        Button btnCloseRight = findViewById(R.id.btn_close_right);
        //点击事件处理
        btnOpenLeft.setOnClickListener((View)->{
            drawerLayout.openDrawer(Gravity.START);
        });
        btnOpenRight.setOnClickListener((View)->{
            drawerLayout.openDrawer(Gravity.END);
        });
        btnCloseRight.setOnClickListener((View)->{
            drawerLayout.closeDrawer(Gravity.END);
        });

        //监听事件
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                Log.i("---", "滑动中");
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                Log.i("---", "打开");
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                Log.i("---", "关闭");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.i("---", "状态改变");
            }
        });
    }
}

关于menu文件中的图标显示问题:

默认:灰色(会将你的图标色重置为灰色)

设为统一颜色

在com.google.android.material.navigation.NavigationView中设置:

app:itemIconTint="@color/blue"

代码中设为图标原色

navigationView.setItemIconTintList(null)

效果图

滑出菜单栏1.jpeg
滑出菜单栏2.jpeg

千夜零一:"之前总是看各种博客学习东西,现在我想用博客记录下我的学习脚步,好东西也需要分享,索取和给予是相互的。以后会尽量日更的!目标完成1001篇博客哈哈。”

  如果觉得对你有所帮助,请不要吝啬你的点赞,有问题也可以在下方评论区留言哦,关注我一起学习吧~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,548评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,069评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,985评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,305评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,324评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,030评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,639评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,552评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,081评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,194评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,327评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,004评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,688评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,188评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,307评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,667评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,337评论 2 358