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

我认为上述答案应该为你工作请尝试一下,让我知道结果。 –

相关问题