2016-12-04 94 views
0

由於某種原因,我的FAB不會像往常一樣進入屏幕的右下角。我在一個片段內,這個片段是一個協調器佈局(我不知道這是否是一種好的做法)。浮動動作按鈕不在右下

這裏是佈局XML代碼:

<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" 
    tools:context="com.gigstudios.polls.fragment.MyPollsFragment"> 

    <LinearLayout 
     android:id="@+id/linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     </android.support.v7.widget.RecyclerView> 

    </LinearLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add_white_36dp" 
     app:fabSize="normal" 
     app:layout_anchor="@id/linear_layout" 
     app:layout_anchorGravity="bottom|right|end" /> 

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

而且由於某些原因,FAB右上角,而不是右下角結束。有誰知道我做錯了什麼?

順便說一句,xml預覽總是在右下角顯示晶圓廠按鈕,但是當我在手機上運行它時,它在右上角。

編輯:這裏是我的main_activity xml來顯示我放碎片(「容器」FrameLayout)的位置。

<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:background="@color/colorGrey"> 

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

     <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/ThemeOverlay.AppCompat.Light" /> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorGrey" 
     android:layout_marginTop="?attr/actionBarSize"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/container"> 
     </FrameLayout> 
    </android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 
+0

只需添加機器人:layout_gravity =「底部|結束」,刪除所有其他重力相關的標記 – Swapnil

+0

嘗試添加額外的FrameLayout請參閱我的答案,如果有幫助 – Swapnil

回答

4

問題是由於android.support.v4.widget.NestedScrollView增加一個字段android:fillViewport="true". Issue was coming as your NestedScrollView`沒有展開以匹配父母或獲取可用空間。我的代碼工作正常,測試

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorPrimary" 
    android:fillViewport="true" 
    android:layout_marginTop="?attr/actionBarSize"> 
</android.support.v4.widget.NestedScrollView> 
+0

不是。仍然沒有工作。 – Jared

+0

請檢查以上編輯的答案,其工作正常我 – Swapnil

+0

是的!簡單的修復。謝謝! – Jared

3

FloatingActionButton刪除線

app:layout_anchor="@id/linear_layout" 
app:layout_anchorGravity="bottom|right|end" 

。他們沒有做任何事情(因爲你正在錨定一個視圖,填滿整個CoordinatorLayout)。這將導致您的layout_gravity受到尊重,並且您的FloatingActionButton將被放置在CoordinatorLayout的右下角。

+0

我做到了,它仍然無法正常工作。順便說一句,XML預覽總是顯示在右下角的晶圓廠按鈕,但是當我在手機上運行它時,它在右上角。 – Jared

+0

你的'MyPollsFragment'你實際使用這個佈局填充屏幕嗎?或者它是否具有'wrap_content'作爲它放置在佈局中的高度? – ianhanniballake

+0

是的,它填滿了屏幕。我將添加我的主要活動xml以顯示我放置片段的位置。 – Jared

2

從浮動操作按鈕的XML代碼刪除android:layout_gravity="bottom|end" ...它會工作