Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize drawer menu in Android Development

Shulou Source: shulou.com Published: 2022-06-02 15:30:53 09月27日 Update

This article mainly introduces the development of Android how to achieve the drawer menu, the article is very detailed, has a certain reference value, interested friends must read it!

Realize the effect

Click the menu chart to enter the drawer

Code implementation

1. Open the app/build.gradle file and add the following to the dependencies closure:

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. Enter the layout layout of the interface where you want to add drawers

Add a DrawerLayout control

First, DrawerLayout is a layout that allows two direct child controls to be placed in the layout. The first child control is the content of the main screen, and the second space is the content displayed in the sliding menu.

All the layout content of the original interface is placed in the first child control.

The sentence "start" of android:layout_gravity= is very important and must be added.

3.NavigationView is used to optimize the sliding menu page

Menu is used to display specific menu items in NavigationView, and headerLayout is used to display the header layout in NavigationView (here I only use menu, so I only write menu)

If there is no menu directory under res, you can create a new menu folder, and then right-click menu- > new_menu resource file

Menu Code:

You can add more than one item, don't forget to reference menu

App:menu= "@ menu/nav_menu"

4. Set click events for the main interface menu chart

Unlike intent,

Setting.setOnClickListener (new View.OnClickListener () {/ / set click event @ Override public void onClick (View v) {mDrawerLayout.openDrawer (GravityCompat.START);}})

5. Set the drawer menu item click event

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;}}); these are all the contents of the article "how to implement the drawer menu in Android development". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Menu content drawer layout control event interface development size code chart file article different important two value interest original multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux macOS vpn Docker