片段内容在导航抽屉中的ActionBar上重叠

Sha*_*hdi 2 android android-fragments android-actionbar navigation-drawer

为什么Fragment布局重叠ActionBar如此

  1. FirstFragment

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">
    
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_first_fragment" />
    
    Run Code Online (Sandbox Code Playgroud)

    在此输入图像描述

  2. SecondFragment

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_second_fragment" />
    
    Run Code Online (Sandbox Code Playgroud)

    在此输入图像描述

activity_navigation

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

    <include
        layout="@layout/app_bar_navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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_navigation"
        app:menu="@menu/activity_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

app_bar_navigation

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.example.sup.slideup.Navigation">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

使用KitKat设备.

小智 8

如果你想保持非的LinearLayout在你的活动布局,你需要设置layout_marginTopFrameLayout保存你的Fragment.在你的app_bar_navigation.xml:

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

<FrameLayout
    android:id="@+id/main_fragment_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize"
    />
...
Run Code Online (Sandbox Code Playgroud)