欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用DrawerLayout完成滑动菜单的示例代码

程序员文章站 2022-07-04 17:02:20
用toolbar编写自定义导航栏,在androidmanifest.xml中你要编滑动菜单的界面处加入如下代码

用toolbar编写自定义导航栏,在androidmanifest.xml中你要编滑动菜单的界面处加入如下代码

<activity android:name=".drawerlayoutactivity"
      android:theme="@style/notitle"></activity>

在values下的styles.xml中加入

<style name="notitle" parent="theme.appcompat.light.noactionbar">//隐藏原有导航栏
    //以下两条均为颜色的设置
    <item name="colorprimary">@color/colorprimary</item>
    <item name="colorprimarydark">@color/colorprimarydark</item>
  </style>

新建两个布局文件(用于主布局文件的调用)
important.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <androidx.appcompat.widget.toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionbarsize"
    android:background="?attr/colorprimary"
    android:theme="@style/themeoverlay.appcompat.dark.actionbar"
    app:popuptheme="@style/themeoverlay.appcompat.light" />
  <imageview
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/b" />
</linearlayout>

left.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="?attr/colorprimary"
  android:padding="40dp">
  <de.hdodenhof.circleimageview.circleimageview//将图片圆形化的控件(design support库中提供)
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@drawable/cat"
    android:scaletype="centercrop"
    android:layout_gravity="center"/>
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margintop="20dp"
    android:text="你的小可爱"
    android:textsize="20sp"
    android:layout_gravity="center"/>
</linearlayout>

将上面圆形化图片控件的库引入到项目中
在app/build.gradle的dependencies闭包中加入如下内容

dependencies {
  implementation filetree(include: ['*.jar'], dir: 'libs')
  implementation 'androidx.appcompat:appcompat:1.2.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'de.hdodenhof:circleimageview:2.1.0'//这是需要添加的
  testimplementation 'junit:junit:4.12'
  androidtestimplementation 'androidx.test.ext:junit:1.1.1'
  androidtestimplementation 'androidx.test.espresso:espressocore:3.2.0'
}

在res目录下新建一个menu文件夹,在menu文件夹下新建一个nav_menu.xml文件,这里的图片是事先准备好的

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  tools:showin="navigation_view">
  <group android:checkablebehavior="single">
    <item
      android:id="@+id/nav_home"
      android:icon="@drawable/ic_menu_camera"
      android:title="home" />
    <item
      android:id="@+id/nav_gallery"
      android:icon="@drawable/ic_menu_gallery"
      android:title="gallery" />
    <item
      android:id="@+id/nav_slideshow"
      android:icon="@drawable/ic_menu_slideshow"
      android:title="slideshow" />
    <item
      android:id="@+id/nav_tools"
      android:icon="@drawable/ic_menu_manage"
      android:title="tools" />
  </group>
</menu>

主布局文件activity_drawer_layout.xml,我们用到了drawerlayout 布局,这里面的navigationview和上面的将图片圆形化的控件一样也需要将库引入到项目中

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/drawer_layout"
  android:fitssystemwindows="true"
  tools:opendrawer="start">
  <include
    layout="@layout/important"//引入上面编好的important.xml文件
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  <com.google.android.material.navigation.navigationview
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitssystemwindows="true"
    app:headerlayout="@layout/left"//引入上面编好的left.xml文件
    app:menu="@menu/nav_menu">//引入上面编好的nav_menu.xml文件
  </com.google.android.material.navigation.navigationview>
</androidx.drawerlayout.widget.drawerlayout>

将上面navigationview的库引入到项目中
在app/build.gradle的dependencies闭包中加入如下内容

dependencies {
  implementation filetree(include: ['*.jar'], dir: 'libs')
  implementation 'androidx.appcompat:appcompat:1.2.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'androidx.navigation:navigation-fragment:2.3.0'//这里是需要添加的
  implementation 'androidx.navigation:navigation-ui:2.3.0'//这里是需要添加的
  implementation 'de.hdodenhof:circleimageview:2.1.0'
  testimplementation 'junit:junit:4.12'
  androidtestimplementation 'androidx.test.ext:junit:1.1.1'
  androidtestimplementation 'androidx.test.espresso:espressocore:3.2.0'

在drawerlayoutactivity.java中编写代码

import androidx.annotation.nonnull;
import androidx.appcompat.app.actionbar;
import androidx.appcompat.app.appcompatactivity;
import androidx.appcompat.widget.toolbar;
import androidx.core.view.gravitycompat;
import androidx.drawerlayout.widget.drawerlayout;

import android.os.bundle;
import android.view.menu;
import android.view.menuitem;
import android.widget.toast;

import com.google.android.material.navigation.navigationview;

public class drawerlayoutactivity extends appcompatactivity {
  private drawerlayout mdrawerlayout;
  private actionbar actionbar;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_drawer_layout);
    mdrawerlayout=findviewbyid(r.id.drawer_layout);//得到drawerlayout实例
    navigationview navigationview=findviewbyid(r.id.nav_view);//获取到navigationview实例
    toolbar toolbar = findviewbyid(r.id.toolbar);//找到toobar控件
    setsupportactionbar(toolbar);//用setsupportactionbar方法将导航栏设置为我们自定义的toolbar   
    actionbar=getsupportactionbar();//用getsupportactionbar()方法实例化导航栏(这里的导航栏就是我们自定义的toolbar)
    if(actionbar!=null){
      actionbar.setdisplayhomeasupenabled(true);//让左上角的导航按钮显示出来
      actionbar.sethomeasupindicator(r.drawable.caidan);//设置导航按钮图标(图片是提前准备好的)
    }
    navigationview.setcheckeditem(r.id.nav_home);//调用navigationview的setcheckeditem方法将home菜单设置为默认选中
    navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() {//设置菜单选中事件的监听器
      @override
      public boolean onnavigationitemselected(@nonnull menuitem menuitem) {
        mdrawerlayout.closedrawers();//选中后关闭菜单
        return true;
      }
    });
  }
   @override
  public boolean onoptionsitemselected(@nonnull menuitem item) {//加入的导航按钮的点击事件
    switch (item.getitemid()){
      case android.r.id.home:
        mdrawerlayout.opendrawer(gravitycompat.start);//调用drawerlayout的opendrawer方法将菜单展示出来
        actionbar=null;
        break;
      default:
    }
    return true;
  }
 }

运行结果展示:

使用DrawerLayout完成滑动菜单的示例代码
使用DrawerLayout完成滑动菜单的示例代码

总结

到此这篇关于使用drawerlayout完成滑动菜单的示例代码的文章就介绍到这了,更多相关drawerlayout完成滑动菜单内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!