整合Android SlidingMenu(SlideMenu)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.