23

我目前有一個FrameLayout的問題在CoordinatorLayout從Android的設計支持庫,而我也跟着從這個post說明在創建標籤。的Android SupportLib - 中的FrameLayout與CoordinatorLayout消耗AppBarLayout整個屏幕高度

基本上大部分的東西如預期,容器碎片膨脹到FrameLayout和他們的製表片段correclty加入ViewPager爲選項卡(需要這種方式,因爲我有很多碎片應該重用佈局) 。

我正在與掙扎的問題是,所述FrameLayout(並且結果也製表片段)消耗整個屏幕的高度,以便它重疊ToolbarTabLayout。爲了可視化的問題,我已經創建了下面的圖像:

Visualized issue

基佈局與CoordinatorLayoutToolbar,和TabLayout

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <include layout="@layout/toolbar" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

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

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

由充氣片段使用分開的佈局到container

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

所有片段由我BaseFragment -class(SO上調用inflater.inflate(getLayoutRes(), null);是導致同樣的問題,這個問題在另一篇文章)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(getLayoutRes(), container, false); 
} 

充氣如果我與AppBarLayout低於正常LinearLayoutFrameLayout開始更換CoordinatorLayout如預期,但根據documentationAppBarLayout其大部分功能需要成爲CoordinatorLayout的直接子。

我可以加marginTopFrameLayout,但我想知道是否有適當的解決方案。預先感謝任何提示!

回答

34

將您的app:layout_behavior="@string/appbar_scrolling_view_behavior"移動到FrameLayout - 該屬性需要位於CoordinatorLayout的直接子節點上。

+0

這解決了我遇到的同樣的問題,謝謝,但我不知道爲什麼。 appbar_scrolling_view_behavior是做什麼的? – Micro

+1

@MicroR - 我建議閱讀[AppBarLayout.ScrollingViewBehavior [(http://developer.android.com/reference/android/support/design/widget/AppBarLayout.ScrollingViewBehavior.html)的Javadoc)或者看[source代碼](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/design/src/android/support/design/widget/AppBarLayout.java#1128) - 它添加了所需的如果放在可滾動視圖上(如'NestedScrollView'或'RecyclerView'),填充不會與AppBarLayout重疊,也不允許AppBarLayout響應滾動。 – ianhanniballake

+0

@ianhanniballake我已經使用過你的建議,但問題是我得到在我的Tablayout和framelayout之間有一些額外的空間我已經應用bk顏色來分析原因,但沒有背景顏色出現在UI – Erum

相關問題