左右效果图如下:

左边


右边


Android SlidingMenu(SlideMenu)是一个开源项目框架。在git上的下载地址:https://github.com/jfeinstein10/SlidingMenu

现在基于Fragment给出一个简单的左边+右边的SlidingMenu(SlideMenu) 。首先要引入第三方库,这不比多说,现到git上把最新的项目代码下载到本地然后导入到工程中,接下来就是导入和写自己的代码。

主Activity:

<span style="font-size:14px;">package zhangphil.slidingmenu;import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Gravity;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MainActivity extends FragmentActivity {private SlidingMenu menu;// id用于区分左边还是右边的侧边滑menuprivate final static String ID = "id";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);menu = new SlidingMenu(this);// 左边和右边均有menu.setMode(SlidingMenu.LEFT_RIGHT);menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);// 左边menu.setMenu(R.layout.left_menu);// 右边menu.setSecondaryMenu(R.layout.right_menu);Fragment leftFragment = TestFragment.newInstance("左边");Fragment rightFragment = TestFragment.newInstance("右边");FragmentManager fm = getSupportFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.replace(R.id.left, leftFragment);ft.commit();// 必须重新再次获得一个FragmentTransaction。否则报错。ft = fm.beginTransaction();ft.replace(R.id.right, rightFragment);ft.commit();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// 按下BACK + 没有重复if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {// 用户按返回键后,切换SlideMenu <-->主界面。menu.toggle(true);return true;}return super.onKeyDown(keyCode, event);}//// 仅仅用于生成测试的Fragment。//public static class TestFragment extends Fragment {public static Fragment newInstance(String id) {Fragment fragment = new TestFragment();Bundle bundle = new Bundle();bundle.putString(ID, id);fragment.setArguments(bundle);return fragment;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// 仅仅显示一个TextView。TextView tv = new TextView(getActivity());tv.setTextColor(Color.BLACK);tv.setText(this.getArguments().getString(ID) + "");tv.setTextSize(60.0f);tv.setGravity(Gravity.CENTER);return tv;}}}</span>


重写了onKeyDown,为了捕捉用户按击返回键的事件,以便切换。


activity_main.xml:

<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >        <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="SlideMenu小Demo" />    </RelativeLayout></span>

left_menu.xml

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">    <FrameLayout            android:id="@+id/left"            android:layout_width="match_parent"          android:layout_height="0px"            android:layout_weight="1">    </FrameLayout></LinearLayout></span>

right_menu.xml

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">    <FrameLayout            android:id="@+id/right"            android:layout_width="match_parent"          android:layout_height="0px"            android:layout_weight="1">    </FrameLayout></LinearLayout></span>


更多相关文章

  1. android viewflipper和tabhost效率对比
  2. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不
  3. Cordova for android如何在App中处理退出按钮事件
  4. changeConfig配置 以及对activity生命周期的影响
  5. 【Android(安卓)Developers Training】 29. 从Activity获得结果
  6. Android开发实践 Intent 解析
  7. Android使用Intent返回上一个Activity时StackOverflowError
  8. Android,一个函数实现上传文件(单个,多文件)
  9. android view(3)事件处理流程

随机推荐

  1. android build (可参考之建立android编译
  2. android 获取短信验证码倒计时
  3. Android通过JNI操作串口《一》
  4. android 数据存取——SharedPreferences
  5. Android NavigationView 侧滑菜单
  6. Gradle:Basic Project
  7. Android获取默认浏览器信息
  8. Android Wear Preview - Get Started Wit
  9. Android 本地推送消息到通知栏 Notificat
  10. Mono登录界面记住密码的控件