2

com.android.support:design庫的最新更新之後,我有一個奇怪的錯誤:浮動操作按鈕錯誤

Correct behavihour

Bug

預期結果是第一個。然而,大約40%的時間,FAB忽略了主播。 這種情況也發生在設計支持庫的新版本25.0.0版本中。

這裏佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
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="match_parent" 
android:layout_gravity="center" 
android:background="@color/black_semi_transparent" 
android:clickable="true" 
android:orientation="vertical" 
android:baselineAligned="false"> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:background="@android:color/transparent" 
    android:onClick="onBackPressed" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="12" 
    android:baselineAligned="false" 
    android:orientation="horizontal"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent" 
     android:onClick="onBackPressed" /> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="18" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/white" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:id="@+id/linearlayout_anchor" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="2" 
       android:background="@color/red_primary_color" 
       android:orientation="vertical" > 

       ... 

      </LinearLayout> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="6" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" > 

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

        ... 

       </LinearLayout> 
      </ScrollView> 
     </LinearLayout> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:clickable="true" 
      android:src="@drawable/ic_more_vert_white_36dp" 
      app:layout_anchor="@id/linearlayout_anchor" 
      app:layout_anchorGravity="bottom|right|end"/> 

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

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent" 
     android:onClick="onBackPressed" /> 

</LinearLayout> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:background="@android:color/transparent" 
    android:onClick="onBackPressed" /> 

</LinearLayout> 

任何想法?

回答

1

是的它是bug。對於解決方法,您可以採取以下方法。

yourView.post(new Runnable() { 
    @Override 
    public void run() { 
     yourView.requestLayout(); 
    } 
}); 

其中「yourView」是錨定視圖。