在ActionBar

时间:2015-08-14 16:19:52

标签: android navigation drawer

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

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

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

1 个答案:

答案 0 :(得分: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"/>

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