【问题标题】:Floating action bar doesn't animate for Snackbar showing up?浮动操作栏没有动画显示 Snackbar?
【发布时间】:2018-07-26 17:40:34
【问题描述】:

在我的 Android 应用程序中,我使用了来自 Clans' 3rd-party 库的浮动操作按钮。喜欢-

<com.github.clans.fab.FloatingActionButton
        android:id="@+id/fab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_help_outline"
        fab:fab_colorNormal="#009688"
        fab:fab_label="Learn How-To Use"
        fab:fab_size="mini" />

当我的快餐栏弹出时,部落工厂不会像谷歌设计库中的默认浮动操作按钮那样动画。

我看到了一些可以解决问题的 proguard 规则。喜欢:

 -keep class android.support.design.widget.** { *; }
 -keep interface android.support.design.widget.** { *; }

我将这些行添加到 proguard-rules.pro 文件中,但根本没有帮助。 谁能建议我做错了什么?

任何建议/建议将不胜感激:)

【问题讨论】:

  • 你的FloatingActionButton 里面的布局是什么?你在用CoordinatorLayout吗?
  • 不,我使用的是RelativeLayout
  • 其实我用 FloatingActionMenu 和 floatingactionbuttons
  • 哦,对不起,我没有意识到你没有使用股票 FloatingActionButton。
  • 我更新了我的答案以使用您正在使用的库。

标签: android android-button floating-action-button


【解决方案1】:

您想到的默认动画是由CoordinatorLayout 及其伴侣Behavior 类提供的。

行为可用于实现各种交互和其他布局修改,从滑动抽屉和面板到滑动可关闭元素和按钮,这些元素在移动和动画时会粘在其他元素上。

您需要进行两项更改。首先,您应该用CoordinatorLayout 包裹您的RelativeLayout,并将您的FloatingActionButton 移出RelativeLayout,使其成为CoordinatorLayout 的直系后代。您的布局应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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">

    ... your RelativeLayout here

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:layout_gravity="bottom|end"
        app:layout_behavior="com.example.stackoverflow.MyBehavior"/>

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

接下来,您必须创建MyBehavior 类。我基本上只是采用了设计库的FloatingActionButton.Behavior 类,并去掉了所有并非绝对必要的东西。这是剩下的:

public class MyBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {

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

    @Override
    public void onAttachedToLayoutParams(CoordinatorLayout.LayoutParams lp) {
        if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
            lp.dodgeInsetEdges = Gravity.BOTTOM;
        }
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
        return false;
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, FloatingActionButton child, int layoutDirection) {
        parent.onLayoutChild(child, layoutDirection);
        return true;
    }

    @Override
    public boolean getInsetDodgeRect(CoordinatorLayout parent, FloatingActionButton child, Rect rect) {
        rect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
        return true;
    }
}

有了这些,我得到了你正在寻找的动画:

【讨论】:

  • 添加类后如何使用?
  • @Asif-UlIslamAkash 将 app:layout_behavior 添加到 FAB 并提供完全限定的类名(即与包一起)。在我的示例中,MyBehavior 类在com.example.stackoverflow 包中,因此属性为app:layout_behavior="com.example.stackoverflow.MyBehavior"
猜你喜欢
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2015-12-13
  • 2016-01-09
  • 2015-10-23
相关资源
最近更新 更多