您现在的位置是:群英 > 开发技术 > 移动开发
Android开发中怎样实现抽屉菜单的效果?
Admin发表于 2021-12-21 17:48:50967 次浏览

    这篇文章给大家分享的是Android开发中抽屉菜单的实现。抽屉菜单比较常见而且实用,因此分享给大家做个参考,文中示例代码介绍的很详细,感兴趣的朋友接下来一起跟随小编看看吧。

实现效果

    点击菜单图表即可进入抽屉

代码实现

    1、打开app/build.gradle文件,在dependencies闭包中添加如下内容:

dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
            compile 'com.android.support:appcompat-v7:24.2.1'
            testCompile 'junit:junit:4.12'
            compile 'com.android.support:design:24.2.1'
            compile 'de.hdodenhof:circleimageview:2.1.0'
        }

2、进入想要添加抽屉的界面的layout布局

    添加DrawerLayout控件

    首先DrawerLayout是一个布局,在布局中允许放入两个直接子控件,第一个子控件是主屏幕中的内容,第二个空间是滑动菜单中显示的内容

    原本的界面所有布局内容就放在第一个子控件中

<?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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/bk_1"
    tools:context="com.luckyxmobile.graphserviceping.MainActivity">

    <!-- 内容区 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/setting"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_marginLeft="8dp"
            android:background="@drawable/ic_baseline_menu1"
            />
        <!--    android:background="@drawable/ic_baseline_menu_24"-->
        <!--原图标宽高 40 52-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="180dp"
            android:id="@+id/graphServicePing"
            android:gravity="center"
            android:text="Graph Service Ping"
            android:textColor="#26C6DA"
            android:textSize="36dp"/>


        <LinearLayout
            android:layout_marginTop="32dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/bloder"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:paddingHorizontal="4dp">
            <!--        android:paddingHorizontal="16dp"-->
<!--            android:layout_height="wrap_content"-->

            <Button
                android:minHeight="50dp"
                android:id="@+id/btn_input"
                android:layout_width="0dp"
                android:layout_weight="8"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:layout_marginLeft="8dp"
                android:background="@null"
                />

            <Button
                android:id="@+id/btn_ping"
                android:background="@null"
                android:layout_weight="4"
                android:text="Ping!"
                android:textColor="#262626"
                android:textSize="25sp"
                android:layout_width="0dp"
                android:layout_height="80dp"
                />
<!--                android:layout_weight="2"-->
<!--            android:layout_width="50dp"-->
<!--            android:layout_height="50dp"-->
        </LinearLayout>
    </LinearLayout>
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:background="@mipmap/bk_1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu">
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

    android:layout_gravity="start"这一句很重要,一定要加上

3.NavigationView用来优化滑动菜单页面的

    menu用来在NavigationView中显示具体的菜单项,headerLayout则用来在NavigationView中显示头布局(这里我只用到了menu,所以我只写menu)

    在res下如果没有menu目录,可以新建一个menu文件夹,然后右键menu->new_menu resource file

    menu代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_setting"
        android:icon="@drawable/ic_launcher_setting_foreground"
        android:title="设置">
    </item>

</menu>

    可以添加多个item,不要忘了引用menu

app:menu="@menu/nav_menu"

4.设置主界面菜单图表的点击事件

    跟intent不同

setting.setOnClickListener(new View.OnClickListener() {  //设置点击事件
            @Override
            public void onClick(View v) {
            mDrawerLayout.openDrawer(GravityCompat.START);
            }
        });

5、设置抽屉菜单item点击事件

DrawerLayout mDrawerLayout;
  
  mDrawerLayout=findViewById(R.id.drawerLayout);

        NavigationView navView=(NavigationView)findViewById(R.id.nav_view);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch(item.getItemId()){
                    case R.id.nav_setting:
                        startActivity(new Intent(MainActivity.this, Setting.class));
                        break;
                }
                mDrawerLayout.closeDrawers();
                return false;
            }
        });

    以上就是Android开发中实现抽屉菜单的介绍,本文只是提供了一种实现思路,代码仅供参考,需要的朋友可以了解看看,希望对大家学习Android开发有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-07-07 17:30:37 
摘要:sync.Once使用起来很简单, 下面是一个简单的使用案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
2022-12-24 11:05:36 
摘要:在php中,可以使用json_encode()函数来将数组转为json数据,语法“json_encode($arr)”;可以用str_replace()或者preg_replace()函数来将双引号转为单引号,只需要在字符串中查找双引号并将其替换为单引号即可,语法“str_replace('"',"'",字符串)”或“preg_replace('/\"/',"'",字符串)”。
2022-11-19 17:51:52 
摘要:本篇文章给大家通过举例来介绍Vue Router路由重定向与别名设置,希望对需要的朋友有所帮助!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部