浮动操作栏没有动画显示Snackbar?

问题描述 投票:1回答:1

在我的Android应用程序中,我使用了Clans'第三方库中的浮动操作按钮。喜欢 -

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

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

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

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

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

任何建议/意见将非常感谢:)

android android-button floating-action-button
1个回答
1
投票

您正在考虑的默认动画由CoordinatorLayout及其伴侣Behavior类提供。

行为可用于实现各种交互和附加布局修改,范围从滑动抽屉和面板到刷卡可忽略的元素和在其移动和动画时粘附到其他元素的按钮。

您需要进行两项更改。首先,你应该用RelativeLayout包裹你的CoordinatorLayout并将你的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;
    }
}

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.