2016-08-05 121 views
1

为什么当我使用CoordinatorLayout而不是当我使用RelativeLayout时底部的额外空间在屏幕计算中得到考虑。CoordinatorLayout&RelativeLayout屏幕空间差异

with CoordinatorLayoutwith RelativeLayout

看到其在协调布局在底部添加了蓝色部分。

<?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="false" 
    tools:context=".MainActivity.controller.MainActivityNew"> 


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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:titleTextColor="@android:color/white" 
      app:layout_scrollFlags="scroll|enterAlways" 
      android:background="?attr/colorPrimary"/> 

      <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/primary" 
      android:layout_below="@+id/toolbar"/> 

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

<FrameLayout 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/frame_container" 
     android:background="@android:color/white" 
     android:layout_width="fill_parent" 
     android:layout_below="@+id/appbar" 
     android:layout_height="match_parent" 

     /> 

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

我将片段添加到有FAB的FrameLayout。在CoordinatorLayout的情况下,我的FAB按钮在导航菜单按钮下隐藏。

什么可能是可能的解决方案。或为什么这个额外的空间被添加到屏幕布局以及如何避免这个额外的底部空间。

回答

0

使用此

在工具栏已添加的应用:layout_scrollFlags

<android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:titleTextColor="@android:color/white" /> 

删除,在工具栏scrollFlags

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

没有必要在CoordinatorLayout 使用layout_below避免使用layout_below。 在这里,我已经添加完整的代码检查该

<?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" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="false"> 

     <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="wrap_content" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/colorAccent" /> 
     </android.support.design.widget.AppBarLayout> 

     <FrameLayout 
      android:id="@+id/frame_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/white" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

仅使用此是不够的答案做简要说明你已经在你的答案用什么。为什么和你做了什么改变。所以他可以通过你的答案来学习。 –

+0

好吧,对不起:) @jaydroider – MathaN

+0

我需要在我的Fragment FAB。为什么我会把这个放在activity_main中?同样它仍然会离开屏幕,因为你可以看到布局获得的高度不在屏幕上,所以重力参数不会工作 –