2016-11-15 65 views
2

我使用下面的代碼作爲RecyclerView中的項目佈局,FloatingActionButton應該位於底端角落,但它出現在頂端開始有時候,我想弄明白爲什麼出現這種情況?Android FloatingActionButton位於錯誤的位置

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="@dimen/record_task_card_width" 
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/record_task_card_margin" 
    android:clickable="true" 
    android:foreground="?attr/selectableItemBackground"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <FrameLayout 
       android:id="@+id/card_background_container" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="2" 
       android:background="@drawable/bg_card"> 

       <ImageView 
        android:id="@+id/record_task_icon" 
        android:layout_width="@dimen/record_task_icon_size" 
        android:layout_height="@dimen/record_task_icon_size" 
        android:layout_gravity="center" 
        android:contentDescription="@string/app_name"/> 

      </FrameLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/record_task_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="@dimen/record_task_info_margin" 
        android:textAppearance="@style/TextAppearance.AppCompat.Title"/> 

       <TextView 
        android:id="@+id/record_task_caption" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="@style/TextAppearance.AppCompat.Caption"/> 

      </LinearLayout> 

     </LinearLayout> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/start_record_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/fab_margin" 
      android:clickable="true" 
      app:layout_anchor="@id/card_background_container" 
      app:layout_anchorGravity="bottom|end|right" 
      app:srcCompat="@drawable/ic_start_record"/> 

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

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

請參閱本http://stackoverflow.com/questions/30675534/coordinatorlayout-layout-anchor-does-not-work – Redman

+0

刪除'應用:layout_anchor = 「@ ID/card_background_container」' – Furqan

+0

添加android:layout_gravity =「bottom | end | right」用於浮動動作按鈕並刪除應用:layout_anchor =「@ id/card_background_container」 –

回答

3

刪除

app:layout_anchor="@id/card_background_container" 
app:layout_anchorGravity="bottom|end|right" 

這一翻譯添加

android:layout_gravity="bottom|end" 

樣品:

<android.support.design.widget.FloatingActionButton 
      android:id="@+id/start_record_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/fab_margin" 
      android:clickable="true" 
      android:layout_gravity="bottom|end" 
      app:srcCompat="@drawable/ic_start_record"/>