2015-12-27 15 views
1

我有碰撞<include>的FrameLayout和應用程序/操作欄碰撞

我的主要XML

<?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"> 

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

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="250dp" 
    android:background="#DDDDDD" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:menu="@menu/activity_main_drawer"/> 

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

我的content.xml

<?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=".main.MainActivity"> 


<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/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</FrameLayout> 

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

添加android:layout_marginTop="?attr/actionBarSize"這所有的分片佈局奇怪的問題解決問題,但爲什麼?我認爲這是因爲在主佈局中使用的包含技術,但由於工具欄始終存在,framelayout是另一個小部件,但它會從應用程序欄頂部開始計算。

回答

2

因爲您正在使用CoordinatorLayout作爲Fragment佈局的父級。 您尚未在此處定義任何顯式行爲,因此它將使用其默認行爲,這就是Fragment佈局位於AppBarLayout「之下」的原因。

一個簡單的方法來解決,這是對layout_behavior屬性添加到fragment_container

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
+1

謝謝!。這是我第一次使用framelayout和協調器佈局。我不能接受它作爲答案,但肯定我會。有一個不錯的星期一綜合徵! –