Android classic slide menu SlidingMenu

Source: Internet
Author: User

Android classic slide menu SlidingMenu

SlidingMenu is an open-source Android development class library that allows developers to easily create slide menus in their Android apps, such as Google +, YouTube, and Facebook App menus. Next let's take a look at SlidingMenu.

Usage

A simple DEMO:

 
 
  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.  

XML configuration method

If you want to use SlidingMenu as view, you can write the xml layout file 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.

Android SlidingMenu

Currently, many android applications have slide menus, which are very effective.

GitHub has an open-source SlidingMenu library, which is easy to use.

SlidingMenu GitHub address: https://github.com/jfeinstein10/slidingmenu. Sliding, combined with ActionBarSherlock, can use more features, ActionBarSherlock GitHub address: https://github.com/JakeWharton/ActionBarSherlock

Next, we will introduce the use of SlidingMenu.

1) download the zip file from GitHub to your local computer and decompress it to obtain a folder named library.

2) Eclipse import Existing Android Code Into Workspace. Right-click the project and choose properties> Android. You can see that the project Is Library.

3) Right-click the project that will use SlidingMenu and choose properties> Android and Library. 2) add the imported project.

4) to ensure that the SlidingMenu library is successfully applied to the project, the libs used by the project and the libs version used by the SlidingMenu library must be consistent, mainly refers to the android-support-v4.jar. If the project libs does not exist or compilation fails, try creating a new libs folder and place it in the android-support-v4.jar. Right-click libs and choose Build Path> Use as Source. It can be solved in general.

5) After step 4th, you can directly use SlidingMenu in the 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.  

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 attributes of SlidingMenu are recorded as follows:

 
 
  1. Menu. setMode (SlidingMenu. LEFT); // you can specify the LEFT sliding menu.
  2. Menu. setTouchModeAbove (SlidingMenu. TOUCHMODE_FULLSCREEN); // you can set the sliding screen range, which can be swiped in full screen mode.
  3. Menu. setShadowDrawable (R. drawable. shadow); // sets the shadow image.
  4. Menu. setShadowWidthRes (R. dimen. shadow_width); // you can specify the width of the Shadow image.
  5. Menu. setBehindOffsetRes (R. dimen. slidingmenu_offset); // remaining width displayed on the home page when SlidingMenu is selected
  6. Menu. setBehindWidth (400); // you can specify the SlidingMenu width.
  7. Menu. setFadeDegree (0.35f); // gradient degree of SlidingMenu sliding
  8. Menu. attachToActivity (this, SlidingMenu. SLIDING_CONTENT); // append SlidingMenu to Activity
  9. Menu. setMenu (R. layout. menu_layout); // sets the layout file of the menu.
  10. Menu. toggle (); // dynamically determines whether SlidingMenu is automatically disabled or Enabled
  11. Menu. showMenu (); // display SlidingMenu
  12. Menu. showContent (); // display content
  13. Menu. setOnOpenListener (onOpenListener); // listen to slidingmenu to open
  14.  
  15. Menu. setOnOpenedListener (onOpenedlistener); listener after slidingmenu is enabled
  16.  
  17. Menu. OnCloseListener (OnClosedListener); // listens to scyclingmenu to close the current event
  18.  
  19. Menu. OnClosedListener (OnClosedListener); // listen to the event after slidingmenu is disabled
  20.  
  21. You can specify the SlidingMenu on both the left and right.
  22. Menu. setMode (SlidingMenu. LEFT_RIGHT); properties, and then set the layout file of the right menu
  23. Menu. setSecondaryMenu (R. layout. menu_fram2); // you can specify the right menu.
  24.  
  25. Menu. setSecondaryShadowDrawable (R. drawable. shadowright); // shadow image on the right menu

Author: Xiaofeng, codenong net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.