FloatingActionButton setVisibility() not working

2019-04-06 06:22发布

I can't hide my FloatingActionButton. Here is my code:

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>

And I'm calling:

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

I'm trying to hide the FAB, but it seems that setVisibility + clearAnimation does not work if the FAB is in a CoordinatorLayout.

Even if I call fab.clearAnimation, the animation is still triggered. Can anyone help me?

5条回答
我只想做你的唯一
2楼-- · 2019-04-06 06:59

I ran into exactly the same issue. It would seem that Google's Android team doesn't want you to have control of the visibility when the FloatingActionButton is anchored to an AppBarLayout, as discussed in this issue - FloatingActionButton Ignores View Visibility

It looks like a viable fix is wrapping the FAB in a FrameLayout and setting the visibility on the wrapper instead, like this:

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

You may however wish to consider whether this is the ideal behaviour. Google advocates recommend that the FAB be visible as soon as the screen is created. If you're hiding it for longer than required to animate it in, consider showing a disabled state instead.

查看更多
疯言疯语
3楼-- · 2019-04-06 07:07

Use show and hide methods to show and hide the Floating Action Button

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

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

// To hide the Floating Action Button
fab.hide();
查看更多
来,给爷笑一个
4楼-- · 2019-04-06 07:12

If your issue is the animation, you could try invalidating the FAB Behavior. As for the visibility, you should null the anchor you have set in your layout:

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);
查看更多
时光不老,我们不散
5楼-- · 2019-04-06 07:16

you can disable it and make it semitransparent like this

fab.setEnabled(false);
fab.setClickable(false);
fab.setAlpha(0.3f);
查看更多
做个烂人
6楼-- · 2019-04-06 07:25
FloatingActionButton layers = (FloatingActionButton) findViewById(R.id.layers);
layers.hide();

It works for me, setVisibility doesn't work for FloatingActionButton as it belongs to another viewGroup that doesn't have setVisibility method.

查看更多
登录 后发表回答