【问题标题】:Show NavigationDrawer on top of ActionBar在 ActionBar 上显示 NavigationDrawer
【发布时间】:2015-08-14 16:19:52
【问题描述】:

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

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

NavigationDrawer 显示在 ViewPager 上方(在 ActionBar 下方),但我希望它显示在 ActionBar 上方。我怎样才能达到这种效果?

【问题讨论】:

    标签: android navigation drawer


    【解决方案1】:

    您可以使用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"/>
    

    一开始我很挣扎,但是一旦我切换到使用工具栏,导航抽屉就会像我想要的那样覆盖工具栏。如果这里的博文和示例代码还不够,请告诉我,我可以继续帮助您完成它。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多