每个 Activity 中的 NavigationDrawer
NavigationDrawer in every Activity
我为我的主屏幕创建了导航抽屉,现在想在每个 Activity 中添加它。如果不将 copy/pasting 代码写入每个 Activity,我该如何做到这一点?下面是我的主屏幕 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>
通常情况下,恕我直言,NavigationDrawer 旨在与 Fragment 一起使用。 main Activity 有它的 NavigationDrawer,这在 Fragment 中很常见。这样每个 Fragment 就不必拥有或管理自己的 NavigationDrawer。
在Activity中:
@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>
找到 mikepenz 使用 MaterialDrawer 的解决方案
我为我的主屏幕创建了导航抽屉,现在想在每个 Activity 中添加它。如果不将 copy/pasting 代码写入每个 Activity,我该如何做到这一点?下面是我的主屏幕 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>
通常情况下,恕我直言,NavigationDrawer 旨在与 Fragment 一起使用。 main Activity 有它的 NavigationDrawer,这在 Fragment 中很常见。这样每个 Fragment 就不必拥有或管理自己的 NavigationDrawer。
在Activity中:
@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>
找到 mikepenz 使用 MaterialDrawer 的解决方案