標籤: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用於區分左邊還是右邊的側邊滑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>
整合Android SlidingMenu(SlideMenu)