2017-02-13 95 views
2

莫因,底部填充,当小吃吧出现

我遇到了一个FAB内CoordinatorLayout一个奇怪的行为。
当出现Snackbar时,FAB向上滑动,但直接粘到Snackbar而没有任何padding(第二幅图像)。
Snackbar消失后,FAB向下移动到屏幕边缘,没有填充(第三张图像),直到几秒钟后,当FAB再次神奇地向上移动时(布局重新验证?),填充恢复正常(第四张图片)。

我从API 16测试这23

step1

step2

step3

step4

我的布局是这样的:

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/pullToRefreshContainer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

     <LinearLayout 
      android:id="@+id/empty" 
      [...] 
     </LinearLayout> 


     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fabAdd" 
      style="@style/MyTheme.FloatingActionButton" 
      app:layout_anchor="@id/recycler" 
      app:layout_anchorGravity="bottom|right|end" 
      app:useCompatPadding="true"/> 

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

的主题,根据如下:

<style name="MyTheme.FloatingActionButton" parent="Widget.Design.FloatingActionButton"> 
    <item name="android:layout_width">56dp</item> 
    <item name="android:layout_height">56dp</item> 
    <item name="android:layout_gravity">bottom|right</item> 
    <item name="android:layout_marginRight">6dp</item> 
    <item name="android:scaleType">center</item> 
</style> 

有没有人有一个线索是什么原因导致这种行为? 我希望FAB始终保留padding

回答

0

我最近遇到了同样的问题。 TL; DR;我解决了它的:

public class MyFabBehavior extends FloatingActionButton.Behavior { 
    public MyFabBehavior() { 
     super(); 
    } 

    public MyFabBehavior(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean getInsetDodgeRect(
      @NonNull CoordinatorLayout parent, 
      @NonNull FloatingActionButton child, 
      @NonNull Rect rect) { 
     return false; 
    } 
} 

说明:

CoordinatorLayout允许其子女设置dodgeInsetEdges。它是一个重力值。当另一个孩子移动到我们视图的矩形中时,如果没有这个参数,它们会重叠。但是,使用dodgeInsetEdges设置的CoordinatorLayout将移动我们的视图以避免与指定方向重叠。 FAB的默认值是底部。

此外CoordinatorLayout允许我们,而不是使用视图的矩形来定义我们自己的。这可以通过在我们的行为中重写getInsetDodgeRect来完成。 FAB这样做,而且问题出现在哪里。

在预棒棒糖设备上或当useCompatPadding设置为true时,FAB将添加用于绘制其阴影的填充。这是必要的,因为对前棒棒糖的看法不能在他们的界限之外画出阴影。

根据FAB默认行为中的注释,添加阴影填充后,FAB应该将自己偏移回原来的位置。然而,这并未发生。另一方面,闪避嵌入矩形仍然偏移,因此在Snackbar覆盖阴影后将FAB推起,然后一直拖到底部。上述解决方案基本上将其恢复为默认值。

在这一点上,我仍然不知道为什么抵消FAB不按预期工作。也许如果有人可以提供更多的信息,可以找到更好的解决方案。