【问题标题】:NavigationDrawer in every Activity每个 Activity 中的 NavigationDrawer
【发布时间】:2021-07-26 10:32:44
【问题描述】:

我为我的 HomeScreen 创建了 NavigationDrawer,现在想将它添加到每个 Activity 中。如果不将代码复制/粘贴到每个活动中,我该如何做到这一点?下面是我的 HomeScreen XML 代码。

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_screen_image"
    android:fitsSystemWindows="true"
    tools:context=".HomeScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/purple_200"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </LinearLayout>

    MY OTHER CODE

    <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:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

【问题讨论】:

    标签: android android-studio android-activity navigation-drawer


    【解决方案1】:

    通常,恕我直言,NavigationDrawer 旨在与 Fragment 一起使用。主 Activity 有其在 Fragment 中常见的 NavigationDrawer。这样每个 Fragment 就不必拥有或管理自己的 NavigationDrawer。

    活动中:

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();
    
        switch (id) {
            case R.id.navigation_item_1:
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new Fragment1()).commit();
                break;
            case R.id.navigation_item_2:
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new Fragment2()).commit();
                break;
            default:
                return false;
        }
    }
    

    layout/main_activity.xml 示例:

    <!-- A DrawerLayout is intended to be used as the top-level content view
         using match_parent for both width and height
         to consume the full space available. -->
    <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorPrimary"
        tools:context=".MainActivity">
    
        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent" />
    
        <!-- The drawer is given a fixed width in dp
             and extends the full height of the container. -->
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:menu="@menu/navigation" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    【讨论】:

      【解决方案2】:

      mikepenz 找到了使用 MaterialDrawer 的解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-14
        • 1970-01-01
        • 2015-08-02
        • 2021-12-08
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多