2016-08-17 93 views
0

大家下午好,在片段中放置FAB

我试图把FAB放在片段的底部,但我不知道我可以放在哪里。

这里是片段的XML代码:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/swipe_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/cars_list_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:id="@+id/scrollable_layout"> 
       <include layout="@layout/write_comment"/> 

       <android.support.v7.widget.RecyclerView 
        android:id="@+id/recyclerview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
      </LinearLayout> 
    </android.support.design.widget.CoordinatorLayout> 
</android.support.v4.widget.SwipeRefreshLayout> 

而FAB:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="end|bottom" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@drawable/ic_plus" /> 

感谢您的帮助!

的回答让我有第一个 “解决方案”,此代码:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/swipe_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.CoordinatorLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/cars_list_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" 
        android:id="@+id/scrollable_layout"> 
        <include layout="@layout/write_comment"/> 

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

       </LinearLayout> 
     </android.support.design.widget.CoordinatorLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_plus" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true"/> 
</RelativeLayout> 

但它不能正常工作:

enter image description here

+0

看到我的回答波纹管。 –

+0

你可以发布更新的xml代码 –

+0

当然,我做到了! –

回答

2

试试这个:

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

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/swipe_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/cars_list_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/scrollable_layout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 


       <android.support.v7.widget.RecyclerView 
        android:id="@+id/recyclerview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" /> 
      </LinearLayout> 
     </android.support.design.widget.CoordinatorLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_photo_camera_white" 
     app:backgroundTint="@color/colorFabButton" /> 
</RelativeLayout> 
+0

我认为这是正确的方式,但什么是: tools:context =“com.firstcry.warehouse.LandingActivity”? 谢谢! –

+0

@ D.Math对不起,我的错误,现在检查更新的答案。 –

+1

@ D.Math - 他简单地添加了“RelativeLayout”类型的父布局。这使您可以控制View的相对于其他视图的位置。例如,'Fab'具有声明'alignParentBottom'和'alignParentLeft'的属性 - 因为父母是'RelativeLayout','Fab'能够理解底部和左边的位置。尽管如此,我唯一的建议是把它放在右边,因为这似乎是常态。 – Subby

0

检查下面的布局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/contactnew"> 

    <include 
     layout="@layout/contentcontact"/> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/facebookfab" 
     android:layout_gravity="bottom|right" 
     android:layout_width="299dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     app:backgroundTint="#fff" 
     android:src="@mipmap/facebook" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/callfab" 
     android:layout_gravity="bottom|left" 
     android:layout_width="299dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:clickable="true" 
     app:backgroundTint="@color/Color_Blue" 

     android:src="@drawable/ic_local_phone_black_24dp" /> 


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

检查此布局 –

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 



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

      <ImageView 
       android:id="@+id/image12" 
       android:layout_width="match_parent" 
       android:layout_height="250dp" 
       android:alpha="0.5" 
       android:src="@drawable/lawbook" 
       android:scaleType="fitXY"/> 
      <de.hdodenhof.circleimageview.CircleImageView 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/profile_image" 

       android:layout_width="120dp" 
       android:layout_height="120dp" 
       android:src="@drawable/aliyar" 

       android:layout_above="@+id/text121" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="71dp" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/image12" 
       android:textSize="14sp" 
       android:layout_marginTop="40dp" 
       android:id="@+id/text121" 
       android:text="@string/aliyar1" 
       android:gravity="center_horizontal" 

       /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text121" 
       android:layout_marginTop="5dp" 
       android:textSize="14sp" 
       android:text="@string/aliyar2" 
       android:id="@+id/text122" 
       android:gravity="center_horizontal" 

       /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text122" 
       android:layout_marginTop="5dp" 
       android:text="@string/aliyar3" 
       android:textSize="14sp" 
       android:id="@+id/text123" 
       android:gravity="center_horizontal" 

       /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text123" 
       android:layout_marginTop="5dp" 
       android:text="@string/aliyar4" 
       android:id="@+id/text124" 
       android:textSize="14sp" 
       android:gravity="center_horizontal" 

       /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text124" 
       android:layout_marginTop="5dp" 
       android:text="@string/aliyar5" 
       android:id="@+id/text125" 
       android:textSize="14sp" 
       android:gravity="center_horizontal" 

       /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/text125" 
       android:layout_marginTop="5dp" 
       android:text="@string/aliyar6" 
       android:textSize="14sp" 
       android:id="@+id/text126" 
       android:gravity="center_horizontal" 

       /> 




     </RelativeLayout> 





    </ScrollView> 

</RelativeLayout>