【问题标题】:FloatingActionButton and weird BehaviorFloatingActionButton 和奇怪的行为
【发布时间】:2017-07-15 20:08:20
【问题描述】:

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

这是一个sn-p:

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;
    }
}

当使用这种行为时,我会在按钮上添加奇怪的边距:

然后,当隐藏按钮并再次显示(在片段之间导航)时,边距会增加,但只会增加一次(每次迭代后不会增加):

这是我的布局:

<?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,那么即使在切换片段(显示/隐藏序列)之后,所有边距也会消失:

但是这个类自己控制snackbar,所以它对我不起作用。

这是在 Nougat 和 KitKat 版本上使用最新(和以前)支持 lib: 25.2.0 测试的。不同之处在于 - 在 KitKat 上没有“第二个班次阶段”。边距从一开始就显示出来。

【问题讨论】:

  • 你尝试输入app:useCompatPadding="false"吗?
  • 你真的需要这种简单情况的行为吗?为什么不对这两个使用垂直线性布局并在常规布局阶段完成所有工作?
  • 谢谢。它对 Nougat 有帮助,但对 KitKat 没有帮助。也许还有其他建议?
  • 关于使用线性布局。我的布局有一个“动态”应用栏和底部栏(它们对滚动有反应)。我也需要对小吃店做出反应。不确定线性布局是否有帮助。

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


【解决方案1】:

尝试将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"/>

【讨论】:

  • 不幸的是它没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 2011-10-04
  • 2015-03-16
  • 2017-07-04
  • 2019-02-16
  • 2013-06-03
相关资源
最近更新 更多