FloatingActionButton always visible

梦想与她 提交于 2020-01-05 12:59:22

问题


I am getting familiar with the new support.design library and I'm facing this issue with the FloatingActionButton. I'm trying to hide it programmatically, but the button stay always visible.

I have the feeling that app:layout_anchor="@id/appbar" must override the visibility of the button.

JAVA:

@InjectView(R.id.search_fab)
FloatingActionButton search_fab;

[...]

search_fab.setVisibility(View.GONE);

XML:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:theme="@style/ToolBarStyle"
        >

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:layout_height="210dp"
            android:id="@+id/top_sales_product_recycler_view"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            android:theme="@style/ToolBarStyle"
            />

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


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

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/category_recycler_view"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:src="@drawable/abc_ic_search_api_mtrl_alpha"
    android:layout_margin="@dimen/fab_margin"
    android:id="@+id/search_fab"
    app:borderWidth="1dp"
    />


回答1:


To get rid of the anchor you can use:

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(View.NO_ID);
fab.setLayoutParams(p);

As far as I know, having an anchor does really, as you say, affect the possibility of hiding the button, so calling this before the visibility change should work.




回答2:


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



回答3:


fab.setVisibility(View.INVISIBLE); Wasn't working.

Using : fab.hide(); solved the problem.



来源:https://stackoverflow.com/questions/31052324/floatingactionbutton-always-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!