Introduction to the use of Android classic sliding menu SlidingMenu

Introduction to the use of Android classic sliding menu SlidingMenu

SlidingMenu is an open source Android development library that allows developers to easily create sliding menus in their Android apps, similar to the app menus of Google+, YouTube, and Facebook. Let's take a look at the effect of SlidingMenu.

How to use

A simple DEMO example:

  1. public   class SlidingExample extends Activity {
  2.  
  3.      @Override  
  4.      public   void onCreate(Bundle savedInstanceState) {
  5.          super .onCreate(savedInstanceState);
  6. setTitle(R.string.attach);
  7.          // set the content view  
  8. setContentView(R.layout.content);
  9.          // configure the SlidingMenu  
  10. SlidingMenu menu = new SlidingMenu( this );
  11. menu.setMode(SlidingMenu.LEFT);
  12. menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
  13. menu.setShadowWidthRes(R.dimen.shadow_width);
  14. menu.setShadowDrawable(R.drawable.shadow);
  15. menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
  16. menu.setFadeDegree( 0 .35f);
  17. menu.attachToActivity( this , SlidingMenu.SLIDING_CONTENT);
  18. menu.setMenu(R.layout.menu);
  19. }
  20.  
  21. }

XML configuration method

If you want to use SlidingMenu as a view, the xml layout file can be written as follows:

  1. <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
  2. xmlns:sliding= "http://schemas.android.com/apk/res-auto"  
  3. android:id= "@+id/slidingmenulayout"  
  4. android:layout_width= "fill_parent"  
  5. android:layout_height= "fill_parent"  
  6. sliding:viewAbove= "@layout/YOUR_ABOVE_VIEW"  
  7. sliding:viewBehind= "@layout/YOUR_BEHIND_BEHIND"  
  8. sliding:touchModeAbove= "margin|fullscreen"  
  9. sliding:behindOffset= "@dimen/YOUR_OFFSET"  
  10. sliding:behindWidth= "@dimen/YOUR_WIDTH"  
  11. sliding:behindScrollScale= "@dimen/YOUR_SCALE"  
  12. sliding:shadowDrawable= "@drawable/YOUR_SHADOW"  
  13. sliding:shadowWidth= "@dimen/YOUR_SHADOW_WIDTH"  
  14. sliding:fadeEnabled= "true|false"  
  15. sliding:fadeDegree= "float"  
  16. sliding:selectorEnabled= "true|false"  
  17. sliding:selectorDrawable= "@drawable/YOUR_SELECTOR" />

Note: You cannot use behindOffset and behindWidth at the same time, otherwise an error will occur.

Introduction to Android SlidingMenu

Now many Android applications have side-sliding menus, which work very well.

There is an open source library of SlidingMenu on GitHub, which is very convenient to use.

SlidingMenu GitHub address: https://github.com/jfeinstein10/SlidingMenu. GitHub says that Sliding can be more functional when combined with ActionBarSherlock. ActionBarSherlock GitHub address: https://github.com/JakeWharton/ActionBarSherlock

Next, let's introduce the use of SlidingMenu.

(1) Download the zip file from GitHub and decompress it to get a folder called library.

(2) Eclipse import Existing Android Code Into Workspace. Right-click the project and select Properties->Android. You can see that it is Library.

(3) Right-click the project that will use SlidingMenu and select Properties->Android. Under Library, add the project imported in (2).

(4) To ensure that the SlidingMenu library is successfully applied to the project, you must ensure that the libs used by the project and the libs used by the SlidingMenu library are consistent in version, mainly referring to android-support-v4.jar. If the project libs does not exist or the compilation fails, you can try to create a new libs folder and put android-support-v4.jar in it. Right-click on libs->Build Path->Use as Source. This can usually solve the problem.

(5) After step (4), you can use SlidingMenu directly in your project.

Java code: MainActivity:

  1. package com.jj.testslidingmenu;
  2.  
  3. import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.view.KeyEvent;
  8. import android.view.Menu;
  9.  
  10. public   class MainActivity extends Activity {
  11.  
  12. SlidingMenu slidingMenu;
  13.      @Override  
  14.      protected   void onCreate(Bundle savedInstanceState) {
  15.          super .onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. slidingMenu = new SlidingMenu( this );
  19. slidingMenu.setMode(SlidingMenu.LEFT);
  20. slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
  21. slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
  22. slidingMenu.setMenu(R.layout.slidingmenu);
  23. slidingMenu.attachToActivity( this , SlidingMenu.SLIDING_CONTENT);
  24. }
  25.  
  26.      @Override  
  27.      public   boolean onKeyDown( int key, KeyEvent event){
  28.          switch (key) {
  29.          case KeyEvent.KEYCODE_MENU:
  30. slidingMenu.toggle( true );
  31.              break ;
  32.  
  33.          default :
  34.              break ;
  35. }
  36.          return   false ;
  37. }
  38.  
  39. }

XML layout code: layout/slidingmenu.xml:

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. android:layout_width= "match_parent"  
  4. android:layout_height= "match_parent"  
  5. android:orientation= "vertical"    
  6. android:background= "#ff999999" >
  7.  
  8. <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
  9. android:id= "@+id/slidingmenu"  
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "match_parent"  
  12. >
  13. <Button
  14. android:layout_width= "match_parent"  
  15. android:layout_height= "wrap_content"  
  16. android:text = "click me" />
  17.  
  18. </com.jeremyfeinstein.slidingmenu.lib.SlidingMenu>
  19. </LinearLayout>

Some common property settings for SlidingMenu are recorded as follows:

  1. menu.setMode(SlidingMenu.LEFT); //Set the left sliding menu  
  2. menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); //Set the sliding screen range. This setting allows sliding in the full screen area.  
  3. menu.setShadowDrawable(R.drawable.shadow); //Set the shadow image  
  4. menu.setShadowWidthRes(R.dimen.shadow_width); //Set the width of the shadow image  
  5. menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); //The remaining width of the main page when SlidingMenu is drawn  
  6. menu.setBehindWidth( 400 ); //Set the width of the SlidingMenu menu  
  7. menu.setFadeDegree( 0 .35f); //The gradient degree when SlidingMenu slides  
  8. menu.attachToActivity( this , SlidingMenu.SLIDING_CONTENT); //Attach SlidingMenu to Activity  
  9. menu.setMenu(R.layout.menu_layout); //Set the menu layout file  
  10. menu.toggle(); //Dynamic judgment to automatically close or open SlidingMenu  
  11. menu.showMenu(); //Show SlidingMenu  
  12. menu.showContent(); //Display content  
  13. menu.setOnOpenListener(onOpenListener); //Listen for slidingmenu to open  
  14.  
  15. menu.setOnOpenedListener(onOpenedlistener); listen for slidingmenu opening
  16.  
  17. menu.OnCloseListener(OnClosedListener); //Listen for events when slidingmenu is closed  
  18.  
  19. menu.OnClosedListener(OnClosedListener); //Listen for events after slidingmenu is closed  
  20.  
  21. You can slide out the SlidingMenu menu on the left or right. Just set
  22. menu.setMode(SlidingMenu.LEFT_RIGHT); property, and then set the layout file of the right menu
  23. menu.setSecondaryMenu(R.layout.menu_fram2); //Set the right menu  
  24.  
  25. menu.setSecondaryShadowDrawable(R.drawable.shadowright); //Shadow image of the right menu  

Original link: http://www.codeceo.com/article/android-slidingmenu-2.html

Author: Xiaofeng from Coder.net

<<:  Exclusive interview with Umeng: Focusing on data services for the mobile development ecosystem

>>:  HTML5 game performance is greatly improved, Egret Engine 1.5 is released

Recommend

IBM Chief Scientist: AlphaGo does not have even 1% of talent

Ever since Google's artificial intelligence A...

Learn Facebook Advertising in One Day (10,000 Words)

Many people ask, is it difficult to learn Faceboo...

5K OS router is undoubtedly the "dark horse" router

The era of Internet big data is striding into a n...

How do content-based products go global from Douyin, Toutiao, UC Headlines, etc.?

2018 marks a new stage for China’s Internet going...

The thumb-sized "girl" left her mother on the fifth day after birth...

Is she the child her mother doesn't want? Is ...

How to use “scenario-based thinking” to write copy?

We often mention "using scenario-based think...

How to make a submersible make sound from the deep sea?

The deep sea has always been beautiful and myster...

Product operation: How to increase the payment rate?

Recently, I occasionally discuss some issues with...