2017-02-24 101 views
0

当使用CoordinatorLayout.Behavior和FloatingActionButton时,发生了一些奇怪的事情。FloatingActionButton和奇怪行为

这里是一个片段:

public class BottomFloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { 
    public BottomFloatingActionButtonBehavior() { 
    } 

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

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
     return (dependency instanceof BottomNavigation); 
    } 

    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
     if (dependency instanceof BottomNavigation) { 
      BottomNavigation bottomNavigation = (BottomNavigation) dependency; 
      int height = bottomNavigation.getNavigationHeight() - bottomNavigation.getBottomInset(); 
      float offset = bottomNavigation.getTranslationY() - height; 
      child.setTranslationY(offset); 
      return true; 
     } 

     return false; 
    } 
} 

当使用这种行为我得到怪异边距添加到按钮: Margins on first frame

然后,隐藏按钮,并再次示出当边缘(片段之间导航)增加,但只有一次(每次迭代后不增加): enter image description here

这是我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<layout 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" 
    tools:context=".Activities.MainActivity"> 

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

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/app_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar_main" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

     <FrameLayout 
      android:id="@+id/frame_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

     <it.sephiroth.android.library.bottomnavigation.BottomNavigation 
      android:id="@+id/navigation_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      app:layout_behavior="@string/bbn_appbar_behavior" 
      app:bbn_entries="@menu/activity_main_navigation" /> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/button_action" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end|bottom" 
      app:borderWidth="0dp" 
      app:fabSize="normal" 
      app:layout_behavior="it.sephiroth.android.library.bottomnavigation.BottomFloatingActionButtonBehavior" 
      app:srcCompat="@drawable/ic_fa_plus_white_24dp" 
      app:useCompatPadding="true"/> 

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

我在做什么错?这是什么行为? 最有趣的是,如果我改变我的超级类FloatingActionButton.Behavior那么所有的利润甚至开关片段(显示/隐藏序列)走了之后: enter image description here

但是这个类本身控制小吃吧所以它赢得了”为我工作。

这是使用最新的(以前也是)支持lib:25.2.0在Nougat和KitKat版本上测试的。不同之处在于 - 在KitKat上不存在“第二个转变阶段”。利润从一开始就显现出来。

+0

您是否尝试将'app:useCompatPadding =“false”'? – nikis

+0

你是否真的需要这种简单情况下的行为?为什么不使用这些2的垂直线性布局,并在常规布局阶段完成所有工作? – nikis

+0

谢谢。它对牛轧糖有帮助,但对KitKat没有帮助。也许有其他建议吗? –

回答

0

尝试设置android:minWidthandroid:minHeight0dp

<android.support.design.widget.FloatingActionButton 
     android:minWidth="0dp" 
     android:minHeight="0dp" 
     android:id="@+id/button_action" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     app:borderWidth="0dp" 
     app:fabSize="normal" 
     app:layout_behavior="it.sephiroth.android.library.bottomnavigation.BottomFloatingActionButtonBehavior" 
     app:srcCompat="@drawable/ic_fa_plus_white_24dp" 
     app:useCompatPadding="true"/> 
+0

不幸的是它没有帮助。 –