FloatingActionButton setVisibility() 方法无效

19

我无法隐藏我的 FloatingActionButton。以下是我的代码:

XML:

<CoordinatorLayout>

    <AppBarLayout android:id="@+id/appbar">

        <CollapsingToolbarLayout>

            <ImageView/>

            <android.support.v7.widget.Toolbar />

        </CollapsingToolbarLayout>

    </AppBarLayout>

    <NestedScrollView />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"/>

</CoordinatorLayout>

我在呼叫:

fab.clearAnimation();
fab.setVisibility(View.GONE);

我想隐藏悬浮操作按钮(FAB),但是如果FAB在CoordinatorLayout中,setVisibility + clearAnimation似乎不起作用。

即使我调用fab.clearAnimation,动画仍然会触发。有人能帮忙吗?


@Clairvoyant fab 是浮动操作按钮(FloatingActionButton)的意思。 - user2331095
尝试在setVisibility下添加fab.invalidate()。或者交换clearAnimation和setVisibility的位置。 - Danielson
你是否在使用@+id,因为hardartcore已经编辑了你的问题。 - Pankaj
@Clairvoyant,我在我的问题中忘记在XML中添加@+id/fab - user2331095
我认为你应该参考以下链接:http://stackoverflow.com/questions/16087913/unable-to-make-view-completely-gone-after-using-translateanimation?answertab=votes#tab-top - CodeMonster
6个回答

49
使用show和hide方法来显示和隐藏浮动操作按钮。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

// To show the Floating Action Button
fab.show();

// To hide the Floating Action Button
fab.hide();

fab.setVisibility() 不同,fab.show()fab.hide() 会触发动画效果。因此,如果您想在隐藏 fab 后启动新活动,则应使用 fab.addOnHideAnimationListener(animatorListener) 并从 animatorListener 的 onAnimationEnd() 回调中启动下一个活动。注意 - 您应该在调用 fab.hide() 之前调用 fab.addOnHideAnimationListener()。如果不按照这些步骤操作,可能无法获得平滑的隐藏动画。 - ban-geoengineering

18

FloatingActionButtonsetVisibility() 方法在使用最新的 Gradle 6.xbuild-tool 28.x.x 进行构建时会出错,并且不再被推荐使用。 相反,应该使用:

fab.hide()    // fab.setVisibility(View.GONE)
fab.show()    // fab.setVisibility(View.VISIBLE)

注意:已在Android Studio 3.4.1Gradle 6.0build-tool 28.0.1上进行了成功测试。


与其创建一个新的(但非常相似的)答案,将这些信息作为评论添加到mstoic的答案中可能更合适:https://dev59.com/AV0Z5IYBdhLWcg3wzC9W#53770108 - ban-geoengineering
除了他,我还添加了setVisibility()不起作用的原因。 - Rahul Raina
1
是的,我知道。 :-) 但我认为将该信息作为注释添加到您的代码中会更合适,因为您的代码(实际解决方案)与他的完全一样。 - ban-geoengineering

15

如果你的问题是动画,你可以尝试使FAB Behavior失效。至于可见性,你应该将在布局中设置的锚点设置为空值:

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(null); //should disable default animations
p.setAnchorId(View.NO_ID); //should let you set visibility
fab.setLayoutParams(p);
fab.setVisibility(View.GONE); // View.INVISIBLE might also be worth trying


//to bring things back to normal state
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setBehavior(new FloatingActionButton.Behavior());
p.setAnchorId(R.id.appbar);
fab.setLayoutParams(p);

1
它对你有效吗?我已经尝试过了,但它不起作用... FAB仍然可见,动画仍然触发... - user2331095
触发动画的是什么?你是指调用setVisibility(GONE)会触发动画,还是这只是你试图避免动画的一种解决方法? - natario
1
如果您使用我代码的前四行仍然没有任何反应? - natario
你是对的。经过测试,我发现只有在将anchorId设置为null时,fab.setVisibility才能起作用。我认为你最好在你的答案中加入fab.setVisibility - user2331095
我遇到了同样的问题,你的答案解决了它。谢谢。 - Hisham Bakr
显示剩余5条评论

6

我遇到了完全相同的问题。看起来Google的Android团队不希望在将FloatingActionButton锚定到AppBarLayout时允许您控制其可见性,正如在这个问题中所讨论的那样 - FloatingActionButton忽略View的可见性

看起来一个可行的解决方案是将FAB放入FrameLayout中,并在包装器上设置可见性,就像这样:

<android.support.design.widget.FrameLayout
    android:id="@+id/fab_container"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:visibility="invisible">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fab_icon"/>

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

然而,您可能需要考虑这是否是理想的行为。Google推荐将FAB在屏幕创建时立即可见。如果您需要隐藏它的时间超过动画所需的时间,请考虑显示禁用状态。


2
您可以像这样禁用它并将其半透明化:
fab.setEnabled(false);
fab.setClickable(false);
fab.setAlpha(0.3f);

2
FloatingActionButton layers = (FloatingActionButton) findViewById(R.id.layers);
layers.hide();

对我而言这很奏效,但是setVisibility对于FloatingActionButton无效,因为它属于另一个没有setVisibility方法的viewGroup


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