经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android开发实现抽屉菜单
来源:jb51  时间:2021/11/15 17:22:14  对本文有异议

本文实例为大家分享了Android开发实现抽屉菜单的具体代码,供大家参考,具体内容如下

实现效果

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

代码实现

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

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

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

添加DrawerLayout控件

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/drawerLayout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:background="@mipmap/bk_1"
  9. tools:context="com.luckyxmobile.graphserviceping.MainActivity">
  10.  
  11. <!-- 内容区 -->
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="vertical">
  16. <ImageView
  17. android:id="@+id/setting"
  18. android:layout_width="56dp"
  19. android:layout_height="56dp"
  20. android:layout_marginLeft="8dp"
  21. android:background="@drawable/ic_baseline_menu1"
  22. />
  23. <!-- android:background="@drawable/ic_baseline_menu_24"-->
  24. <!--原图标宽高 40 52-->
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_gravity="center"
  29. android:layout_marginTop="180dp"
  30. android:id="@+id/graphServicePing"
  31. android:gravity="center"
  32. android:text="Graph Service Ping"
  33. android:textColor="#26C6DA"
  34. android:textSize="36dp"/>
  35.  
  36.  
  37. <LinearLayout
  38. android:layout_marginTop="32dp"
  39. android:layout_gravity="center_horizontal"
  40. android:background="@drawable/bloder"
  41. android:layout_width="match_parent"
  42. android:layout_height="80dp"
  43. android:layout_marginStart="16dp"
  44. android:layout_marginEnd="16dp"
  45. android:paddingHorizontal="4dp">
  46. <!-- android:paddingHorizontal="16dp"-->
  47. <!-- android:layout_height="wrap_content"-->
  48.  
  49. <Button
  50. android:minHeight="50dp"
  51. android:id="@+id/btn_input"
  52. android:layout_width="0dp"
  53. android:layout_weight="8"
  54. android:layout_height="wrap_content"
  55. android:textSize="20dp"
  56. android:layout_marginLeft="8dp"
  57. android:background="@null"
  58. />
  59.  
  60. <Button
  61. android:id="@+id/btn_ping"
  62. android:background="@null"
  63. android:layout_weight="4"
  64. android:text="Ping!"
  65. android:textColor="#262626"
  66. android:textSize="25sp"
  67. android:layout_width="0dp"
  68. android:layout_height="80dp"
  69. />
  70. <!-- android:layout_weight="2"-->
  71. <!-- android:layout_width="50dp"-->
  72. <!-- android:layout_height="50dp"-->
  73. </LinearLayout>
  74. </LinearLayout>
  75. <com.google.android.material.navigation.NavigationView
  76. android:id="@+id/nav_view"
  77. android:background="@mipmap/bk_1"
  78. android:layout_height="match_parent"
  79. android:layout_width="match_parent"
  80. android:layout_gravity="start"
  81. app:menu="@menu/nav_menu">
  82. </com.google.android.material.navigation.NavigationView>
  83.  
  84. </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代码:

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

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

  1. app:menu="@menu/nav_menu"

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

跟intent不同

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

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

  1. DrawerLayout mDrawerLayout;
  2. mDrawerLayout=findViewById(R.id.drawerLayout);
  3.  
  4. NavigationView navView=(NavigationView)findViewById(R.id.nav_view);
  5. navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
  6.  
  7. @Override
  8. public boolean onNavigationItemSelected(MenuItem item) {
  9. switch(item.getItemId()){
  10. case R.id.nav_setting:
  11. startActivity(new Intent(MainActivity.this, Setting.class));
  12. break;
  13. }
  14. mDrawerLayout.closeDrawers();
  15. return false;
  16. }
  17. });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号