1

我使用協調器佈局,回收站視圖和浮動按鈕。不知何故,fab按鈕不會停留在佈局的底部。我希望按鈕即使在滾動時也能保持在最底部。我所知道的是,晶圓廠按鈕在回收視圖中佔據最後一項的底部位置。如何讓浮動按鈕保持在佈局的底部?

enter image description here

列表activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
      tools:listitem="@layout/activity_sample_05borangb_parent" 
      android:id="@+id/recyclerView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="vertical"/> 

    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:foregroundGravity="bottom" 
     android:layout_gravity="bottom|end"/> 


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

刪除'安卓scrollbars'或'安卓foregroundGravity'並有一個嘗試。 – KeLiuyue

+0

@KeLiuYue我試過了......但按鈕仍然在佈局的中間盤旋 – xxmilcutexx

回答

3

enter image description here

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:foregroundGravity="bottom" 
     android:layout_margin="16dp" 
     android:layout_gravity="bottom|end"/> 


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

它仍然無法正常工作...按鈕仍然顯示在列表底部而不是佈局的底部顯示您的努力 – xxmilcutexx

+0

。解釋你在哪裏得到錯誤@ Pritesh-ɥsǝʇᴉɹꓒ –

2

做如下修改

<android.support.design.widget.CoordinatorLayout 
    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" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

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

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

    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:fabSize="normal" 
     app:layout_anchor="@id/container" 
     app:layout_anchorGravity="bottom|end"/> 


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

你需要指定一個layout_anchorlayout_anchorGravity

+0

我試過了......它不起作用。該按鈕仍然位於回收站視圖的底部,而不是頁面佈局的底部。 – xxmilcutexx

2

您不需要使用android.support.design.widget.CoordinatorLayout來達到此相對佈局綽綽有餘。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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"> 

     <android.support.v7.widget.RecyclerView 
      tools:listitem="@layout/activity_sample_05borangb_parent" 
      android:id="@+id/recyclerView2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 
+0

由於我的recyclerView有兩個項目...浮動按鈕仍然顯示在最後一項的底部,而不是佈局底部 – xxmilcutexx

+0

我認爲上述答案應該爲你工作請嘗試一下,讓我知道結果。 –

相關問題