FloatingActionButton doesn't hide

后端 未结 17 898
星月不相逢
星月不相逢 2020-11-30 03:42

I am trying to hide my FloatingActionButton fabLocation programmatically with :

fabLocation.setVisibility(View.GONE)

but it do

相关标签:
17条回答
  • 2020-11-30 04:12

    If you want to show hidden FAB

        <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/coordinator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
    
    
        <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:fitsSystemWindows="true">
        ...
    
        </android.support.design.widget.AppBarLayout>
    
        ...
    
        <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:visibility="gone"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:clickable="true"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    and

        CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
        p.anchorGravity = Gravity.BOTTOM | Gravity.END;
        p.setAnchorId(R.id.appbar);
        p.setMargins(...);
        fab.setLayoutParams(p);
        fab.setVisibility(View.VISIBLE);
    
    0 讨论(0)
  • 2020-11-30 04:12

    I worked around the show()/hide() shortcoming by placing the FAB in or out of the screen using layout margins. example:

    CoordinatorLayout.LayoutParams p =
       new CoordinatorLayout.LayoutParams(
          CoordinatorLayout.LayoutParams.WRAP_CONTENT,
          CoordinatorLayout.LayoutParams.WRAP_CONTENT);
    
    p.gravity = Gravity.BOTTOM | Gravity.LEFT;
    int fabMargin = (int)res.getDimension(R.dimen.fab_margin);
    
    if( enabled ) {
       p.setMargins(fabMargin,0,0,fabMargin);
    }
    else {
       p.setMargins(-200,0,0,fabMargin);
    }
    mFab.setLayoutParams(p);
    
    0 讨论(0)
  • 2020-11-30 04:14

    It is due to the app:layout_anchor attribute. You must get rid of the anchor before changing visibility:

    CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    p.setAnchorId(View.NO_ID);
    fab.setLayoutParams(p);
    fab.setVisibility(View.GONE);
    
    0 讨论(0)
  • 2020-11-30 04:16

    you can do it using this:

    fab.setEnabled(false);
    fab.setClickable(false);
    fab.setAlpha(0.0f);
    

    This is similar behavior as View.Gone

    0 讨论(0)
  • 2020-11-30 04:23

    I you use this code:

    boolean mFabShouldBeShown;
    FloatingActionButton.OnVisibilityChangedListener fabListener = new FloatingActionButton.OnVisibilityChangedListener() {
            @Override
            public void onShown(FloatingActionButton fab) {
                super.onShown(fab);
                if(!mFabShouldBeShown){
                    fab.hide();
                }
            }
    
            @Override
            public void onHidden(FloatingActionButton fab) {
                super.onHidden(fab);
                if(mFabShouldBeShown){
                    fab.show();
                }
            }
    };
    
    public void methodWhereFabIsHidden() {
        mFabShouldBeShown = false;
        mFloatingActionButton.hide(fabListener);
    }
    
    public void methodWhereFabIsShown() {
        mFabShouldBeShown = true;
        mFloatingActionButton.show(fabListener);
    }
    
    0 讨论(0)
  • 2020-11-30 04:24

    The most simplistic way to hide and show a floating action button would be to call this in your activity. This will also properly animate your FAB automatically.

    Hide:

    nameOfYourFAB.Hide();
    

    Show:

    nameOfYourFAB.Show();
    
    0 讨论(0)
提交回复
热议问题