2016-08-17 41 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>