Viewpager + DrawerLayout + NavigationView无法正确显示

时间:2015-06-19 07:40:09

标签: android navigation-drawer

activity_main.xml中

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start" />

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

fragment_with_tab.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="@color/gitcafe_primary_color">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/gitcafe_primary_color"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <com.gitcafe.uibase.view.SlidingTabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

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

我使用getSupportFragmentManager().beginTransaction().replace(container, fragment).commit来显示片段,但抽屉没有像pic那样正确显示。

如何解决这个问题?

截屏:  http://i.stack.imgur.com/pD2Qn.png  http://i.stack.imgur.com/B1beb.png

ViewPager中的第二个片段是透明的,因此我们可以看到抽屉位于片段下。

1 个答案:

答案 0 :(得分:0)

我不确定为什么会发生这种情况,因为我也有相同的结构,但从未遇到过这个错误。

首先确保你的container var指向正确的容器ID(我假设它是,但只是为了确定)。

还可以尝试在DrawerLayout中设置android:fitsSystemWindows="true"