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