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:
- public class SlidingExample extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle(R.string.attach);
- // set the content view
- setContentView(R.layout.content);
- // configure the SlidingMenu
- SlidingMenu menu = new SlidingMenu(this);
- menu.setMode(SlidingMenu.LEFT);
- menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- menu.setShadowWidthRes(R.dimen.shadow_width);
- menu.setShadowDrawable(R.drawable.shadow);
- menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
- menu.setFadeDegree(0.35f);
- menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
- menu.setMenu(R.layout.menu);
- }
-
- }
XML configuration method
If you want to use SlidingMenu as view, you can write the xml layout file as follows:
- <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
- xmlns:sliding="http://schemas.android.com/apk/res-auto"
- android:id="@+id/slidingmenulayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- sliding:viewAbove="@layout/YOUR_ABOVE_VIEW"
- sliding:viewBehind="@layout/YOUR_BEHIND_BEHIND"
- sliding:touchModeAbove="margin|fullscreen"
- sliding:behindOffset="@dimen/YOUR_OFFSET"
- sliding:behindWidth="@dimen/YOUR_WIDTH"
- sliding:behindScrollScale="@dimen/YOUR_SCALE"
- sliding:shadowDrawable="@drawable/YOUR_SHADOW"
- sliding:shadowWidth="@dimen/YOUR_SHADOW_WIDTH"
- sliding:fadeEnabled="true|false"
- sliding:fadeDegree="float"
- sliding:selectorEnabled="true|false"
- 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:
- package com.jj.testslidingmenu;
-
- import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.KeyEvent;
- import android.view.Menu;
-
- public class MainActivity extends Activity {
-
- SlidingMenu slidingMenu;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- slidingMenu = new SlidingMenu(this);
- slidingMenu.setMode(SlidingMenu.LEFT);
- slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
- slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
- slidingMenu.setMenu(R.layout.slidingmenu);
- slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
- }
-
- @Override
- public boolean onKeyDown(int key, KeyEvent event){
- switch (key) {
- case KeyEvent.KEYCODE_MENU:
- slidingMenu.toggle(true);
- break;
-
- default:
- break;
- }
- return false;
- }
-
- }
XMl layout code: layout/slidingmenu. xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#ff999999">
-
- <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
- android:id="@+id/slidingmenu"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text = "click me"/>
-
- </com.jeremyfeinstein.slidingmenu.lib.SlidingMenu>
- </LinearLayout>
Some common attributes of SlidingMenu are recorded as follows:
- Menu. setMode (SlidingMenu. LEFT); // you can specify the LEFT sliding menu.
- Menu. setTouchModeAbove (SlidingMenu. TOUCHMODE_FULLSCREEN); // you can set the sliding screen range, which can be swiped in full screen mode.
- Menu. setShadowDrawable (R. drawable. shadow); // sets the shadow image.
- Menu. setShadowWidthRes (R. dimen. shadow_width); // you can specify the width of the Shadow image.
- Menu. setBehindOffsetRes (R. dimen. slidingmenu_offset); // remaining width displayed on the home page when SlidingMenu is selected
- Menu. setBehindWidth (400); // you can specify the SlidingMenu width.
- Menu. setFadeDegree (0.35f); // gradient degree of SlidingMenu sliding
- Menu. attachToActivity (this, SlidingMenu. SLIDING_CONTENT); // append SlidingMenu to Activity
- Menu. setMenu (R. layout. menu_layout); // sets the layout file of the menu.
- Menu. toggle (); // dynamically determines whether SlidingMenu is automatically disabled or Enabled
- Menu. showMenu (); // display SlidingMenu
- Menu. showContent (); // display content
- Menu. setOnOpenListener (onOpenListener); // listen to slidingmenu to open
-
- Menu. setOnOpenedListener (onOpenedlistener); listener after slidingmenu is enabled
-
- Menu. OnCloseListener (OnClosedListener); // listens to scyclingmenu to close the current event
-
- Menu. OnClosedListener (OnClosedListener); // listen to the event after slidingmenu is disabled
-
- You can specify the SlidingMenu on both the left and right.
- Menu. setMode (SlidingMenu. LEFT_RIGHT); properties, and then set the layout file of the right menu
- Menu. setSecondaryMenu (R. layout. menu_fram2); // you can specify the right menu.
-
- Menu. setSecondaryShadowDrawable (R. drawable. shadowright); // shadow image on the right menu
Author: Xiaofeng, codenong net