android - 在 ActionBar 顶部显示 NavigationDrawer

标签 android navigation drawer

我正在使用带有自定义操作栏的 Androidhibe NavigationDrawer 和 SwipeTab,如下所示:

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

NavigationDrawer 显示在 ViewPager 上方(在 ActionBar 下方),但我希望它显示在 ActionBar 上方。我怎样才能实现这种影响?

最佳答案

您可以使用 Toolbar 而不是 ActionBar 来实现这一点。

Here is a blog post这可能会帮助您切换到工具栏。

MainActivity 的 XML 看起来像这样:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

        <!-- 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"/>

    </LinearLayout>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.app.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

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

我的工具栏的 XML 如下所示:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"/>

一开始我很纠结,但是一旦我切换到使用工具栏,抽屉导航就会按照我的意愿覆盖工具栏。如果此处的博文和示例代码还不够,请告诉我,我可以继续帮助您解决问题。

关于android - 在 ActionBar 顶部显示 NavigationDrawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014509/

相关文章:

iOS:滑动 UIView 开/关屏幕

android - Doze 模式如何影响注册的监听器(尤其是传感器监听器)

android - Firebase 性能未显示/列出 Android 应用程序中使用的某些端点

android - Firebase 预定通知

ios - 具有 MVVM 架构的 Swiftui 在复杂应用程序中导航屏幕

android - 从操作栏微调器导航中获取所选项目

react-native - react 导航 (V2) : How to set the Icon and the label of a stack Navigator inside a Drawer Navigator?

java - Android 应用程序在主 Activity 启动时强制崩溃

html - 如何使响应式导航菜单保持在页面顶部?

reactjs - material-ui v1.0 中的抽屉不适用于 typescript