禁用FAB淡入淡出动画

4
我有一个布局。
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.myapplication.ScrollingActivity">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <include layout="@layout/content_scrolling"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

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

当我向上或向下滚动页面时,浮动操作按钮会淡入淡出。是否可以禁用这个淡入淡出的动画效果?(视频链接请点击上方video查看)

你想要什么?带有固定FAB的可折叠工具栏?它应该缩小尺寸吗? - Gaket
对于未来的读者:这个答案解释了问题。我发现这个问题+答案是因为我试图弄清楚为什么FloatingActionButton的behavior_autoHide属性不起作用。 - amcnabb
2个回答

7
我找到了它 :-) FloatingActionButtonBehavior有一个behavior_autoHide选项。
问题在于我尝试在布局xml中设置此选项。但是FloatingActionButton.Behavior没有读取此选项,因为Android调用了错误的构造函数。
public Behavior() {
    super();
    mAutoHideEnabled = AUTO_HIDE_DEFAULT;
}

你需要在布局 XML 中设置 Behavior。这样,Android 就可以调用正确的构造函数,并读取 behavior_autoHide 标志。

public Behavior(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.FloatingActionButton_Behavior_Layout);
    mAutoHideEnabled = a.getBoolean(
            R.styleable.FloatingActionButton_Behavior_Layout_behavior_autoHide,
            AUTO_HIDE_DEFAULT);
    a.recycle();
}

您的布局XML文件中重要部分应该如下所示:

<android.support.design.widget.FloatingActionButton
    app:behavior_autoHide="false"
    app:layout_anchor="@id/appbar"
    app:layout_behavior="android.support.design.widget.FloatingActionButton$Behavior"
    app:layout_anchorGravity="bottom|right|end"/> 

3
е№Іеҫ—еҘҪгҖӮд»…д»…ж·»еҠ app:layout_behaviorиҝҷдёҖиЎҢе°ұи¶ід»Ҙи§ЈеҶіиҝҷдёӘbugпјҢзңҹжҳҜеӨӘжЈ’дәҶгҖӮ - amcnabb

0
您可以尝试使用全文搜索: Ctrl+Shift+F查找 "FloatingActionButton.Behavior"。该类的继承者定义了FAB的行为,因此您可以删除处理这种淡出效果的行。 如果没有找到,尝试查找只包含 "Behavior" 的情况,以防其应用于Coordinator布局而不是直接应用于FAB。

我研究了一下这个行为,它有一个自动隐藏选项,但这不是我想要的(或者我可能使用方法不对)。 - Ralph Bergmann
请问您能否将这个类也加入到您的问题中? - Gaket

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接