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

时间:2015-12-06 08:35:07

标签: 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" />
    

    enter image description here

  2. SecondFragment

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_second_fragment" />
    

    enter image description here

  3. 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>
    

    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>
    

    使用KitKat设备。

1 个答案:

答案 0 :(得分:8)

如果您想在Activity布局中维护非LinearLayout,则需要在layout_marginTop上设置一个FrameLayout来保存您的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"
    />
...