带标签的 NavigationDrawer

NavigationDrawer with tabs

我想实现 NavigationDrawer 与选项卡的组合,例如在 PlayStore 应用中:

所以基本思路是有一个android.support.v4.widget.DrawerLayout as the AppCompatActivity's root element, and that contains a android.support.design.widget.NavigationView和主要内容(由一个FrgmentTransaction切换):

<android.support.v4.widget.DrawerLayout >

    <FrameLayout 
        android:id="@+id/main_content"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/main_navigation"
        app:menu="@menu/main_navigation" />

</android.support.v4.widget.DrawerLayout>

选项卡是这样实现的:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

        <android.support.v7.widget.Toolbar />

        <android.support.design.widget.TabLayout />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_tabbed_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

但我不知道如何将它们融合在一起:

如果我需要澄清一些事情,请发表评论。

您应该将导航视图中的每个页面实现为单独的 Fragment。这样,您不必在其中任何一个中包含 Toolbar,因为根 Activty's Toolbar 将始终显示。

就性能而言,我使用过类似的模式并且表现不错,我认为 Android 平台可以处理它,只要您负责任地做其他事情。

以下是您如何组织的示例:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.myapplication.ui.activity.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <include layout="@layout/widget_toolbar" />

            </android.support.design.widget.AppBarLayout>

        </android.support.design.widget.CoordinatorLayout>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/coordinator_layout" />
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

A RelativeLayoutToolbar 下方包含 fragment_container,因此无论将哪个 Fragment 放入 [=16],都会显示相同的 Toolbar =]因为它属于Activity.