FloatingActionButton 始终可见

FloatingActionButton always visible

我正在熟悉新的 support.design 库,我正面临 FloatingActionButton 的这个问题。我试图以编程方式隐藏它,但按钮始终可见。

我觉得 app:layout_anchor="@id/appbar" 必须覆盖按钮的可见性。

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

要摆脱锚点,您可以使用:

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

据我所知,正如您所说,拥有锚点确实会影响隐藏按钮的可能性,因此在可见性更改之前调用它应该有效。

如果要显示隐藏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>

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

fab.setVisibility(View.INVISIBLE); 没用。

使用:fab.hide(); 解决了问题。