FloatingActionButton和奇怪的行为

时间:2017-02-24 20:48:39

标签: android android-support-library floating-action-button android-snackbar

使用带有FloatingActionButton的CoordinatorLayout.Behavior时,我发生了一些奇怪的事情。

这是一个片段:

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上没有“第二班次”。利润率从一开始就显示出来。

1 个答案:

答案 0 :(得分:0)

尝试将android:minWidthandroid:minHeight设为0dp

    <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"/>