首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android 側邊欄選單實現:推薦使用 SlidingMenu 開源框架

     編輯:程式設計疑難解答

主流方案對比

方案特點適用場景
SlidingMenu (jfeinstein10)經典、穩定、Activity/Fragment 均支援老專案維護、快速接入
NavigationView + DrawerLayoutGoogle 官方 Material Design 組件、現代化新專案、遵循 MD 規範
自定義 View + 手勢檢測完全可控、學習成本高特殊交互需求

快速接入 SlidingMenu (GitHub)

  1. 依賴:implementation 'com.jeremyfeinstein.slidingmenu:library:1.3.1' (或複製源碼模組)
  2. 主 Activity:
    public class MainActivity extends SlidingFragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setBehindContentView(R.layout.menu_frame); // 設定側邊欄佈局
            getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
            getSupportFragmentManager().beginTransaction()
                .replace(R.id.menu_frame, new MenuFragment()).commit();
        }
    }

現代化首選:NavigationView

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="..."
    xmlns:app="..."
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 主內容 -->
    <FrameLayout android:id="@+id/content_frame" ... />

    <!-- 側邊欄 -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

建議

**新專案直接使用 NavigationView + DrawerLayout**,符合 Material Design、官方長期維護、與 Fragment/Navigation Component 無縫整合。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved