标签:android
左右效果图如下:
左边
右边
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用于区分左边还是右边的侧边滑menu private final static String ID = "id"; @Override public 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(); } @Override public 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; } @Override public 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>
集成Android SlidingMenu(SlideMenu)
标签:android
原文地址:http://blog.csdn.net/zhangphil/article/details/44078805