0

我有這個問題來適應我的浮動按鈕,我不明白爲什麼它不起作用。 我很感激你的迴應,因爲我無法找到發生這種情況的原因,當滾動或重新加載片段時,一些浮動按鈕將被容納。 對不起,我的英語。在CardView和RecyclerView上添加浮動按鈕

enter image description here

<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="wrap_content" 
android:background="@color/colorBackground" 
android:orientation="vertical"> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/mRequestCard" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:elevation="100dp" 
     android:orientation="horizontal" 
     app:cardBackgroundColor="@color/colorWhite" 
     app:cardCornerRadius="3dp"> 

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

      <View 
       android:id="@+id/vDivisor" 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@color/colorGreen" /> 
        ... 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 

</android.support.v4.widget.NestedScrollView> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:layout_marginRight="@dimen/fab_margin" 
    android:backgroundTint="@color/colorPurple" 
    android:scaleType="centerCrop" 
    android:src="@drawable/ic_route_direction" 
    app:layout_anchor="@id/mRequestCard" 
    app:layout_anchorGravity="right|end|bottom" /> 

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

這是我RecyclerView這是一個片段

<FrameLayout 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" 
tools:context="com.test.HistorialFragment"> 

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

膨脹,這是我的片段,我在那裏展示RecyclerView

<FrameLayout 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:id="@+id/content_frame" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.test.MainActivity" 
tools:showIn="@layout/app_bar_main"> 
</FrameLayout> 
+0

我認爲你應該嘗試在浮動按鈕android:layout_gravity =「bottom | end」 –

回答

0

你應該嘗試RelativeLayout像這樣

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:backgroundTint="@color/colorBlack" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true"/> 
    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:cardCornerRadius="@dimen/activity_corner" 
     android:layout_marginBottom="@dimen/activity_margin"> 
     <!--your cardview item here--> 
    </android.support.v7.widget.CardView> 
</RelativeLayout> 

RecyclerView項目

0

檢查這可以幫助你:

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorPrimary" 
    android:orientation="vertical"> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/mRequestCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:elevation="100dp" 
      android:orientation="horizontal" 
      app:cardBackgroundColor="@color/colorWhite" 
      app:cardCornerRadius="3dp"> 

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

       <View 
        android:id="@+id/vDivisor" 
        android:layout_width="match_parent" 
        android:layout_height="2dp" 
        android:background="@color/colorGreen" /> 
       ... 

      </RelativeLayout> 

     </android.support.v7.widget.CardView> 

    </android.support.v4.widget.NestedScrollView> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:backgroundTint="@color/colorAccent" 
     android:scaleType="centerCrop" 
     android:src="@drawable/ic_launcher" 
     app:layout_anchor="@id/mRequestCard" 
     app:layout_anchorGravity="right|end|bottom" /> 

</RelativeLayout> 
0

嘗試這種使用RelativeLayout,讓您的FloatingActionButton財產android:layout_alignParentBottom="true" android:layout_alignParentRight="true"如下面的代碼

<RelativeLayout android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:backgroundTint="@color/colorBlack" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true"/> 
<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:cardCornerRadius="@dimen/activity_corner" 
    android:layout_marginBottom="@dimen/activity_margin"> 


</android.support.v7.widget.CardView> 

</RelativeLayout>