我有这个布局(没有根CoordinatorLayout的锚点):
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#F5F5F5">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:visibility="gone"
android:fitsSystemWindows="true"
app:elevation="0dp"
tools:visibility="visible">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="@android:color/white">
<android.support.v7.widget.RecyclerView
android:id="@+id/product_images_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:visibility="visible"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
tools:visibility="visible" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/BlackAccentToolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/product_card_content"
android:id="@+id/content_constraint_layout"
android:visibility="gone"
tools:visibility="visible" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/cart_fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:clickable="true"
android:visibility="gone"
app:layout_anchor="@+id/app_bar_layout"
app:layout_anchorGravity="bottom|end"
app:backgroundTint="@color/kupivip_pink_primary"
app:elevation="8dp"
app:fabSize="normal"
app:srcCompat="@drawable/add_to_cart"
app:layout_behavior="android.support.design.widget.FloatingActionButton$Behavior"
app:behavior_autoHide="false"
tools:visibility="visible" />
<View
android:id="@+id/cart_fab_top_margin_view"
android:layout_width="8dp"
android:layout_height="64dp"
app:layout_anchor="@+id/cart_fab"
app:layout_anchorGravity="center_horizontal" />
<View
android:id="@+id/cart_fab_left_margin_view"
android:layout_width="64dp"
android:layout_height="8dp"
app:layout_anchor="@+id/cart_fab"
app:layout_anchorGravity="start|center_vertical" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/favorite_fab"
android:layout_width="40dp"
android:layout_height="40dp"
android:clickable="true"
android:visibility="gone"
android:scaleType="center"
app:layout_anchor="@+id/cart_fab_top_margin_view"
app:layout_anchorGravity="center_horizontal"
app:backgroundTint="@android:color/white"
app:elevation="8dp"
app:fabSize="normal"
app:srcCompat="@drawable/favorite"
tools:visibility="visible" />
<ProgressBar
android:id="@+id/loading_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
tools:visibility="gone" />
<com.roughike.bottombar.BottomBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
app:bb_tabXmlResource="@xml/bottombar_tabs" />
</android.support.design.widget.CoordinatorLayout>
我想动态更改与CollapsingToolbar事件对应的favorite_fab锚点:
private void setupAppBar() {
appBarLayout.addOnOffsetChangedListener(new AppBarStateListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, int state) {
Drawable navigationIcon = toolbar.getNavigationIcon();
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
CoordinatorLayout.LayoutParams favoriteFabLayoutParams =
(CoordinatorLayout.LayoutParams) favoriteFab.getLayoutParams();
if (state == AppBarStateListener.EXPANDED && navigationIcon != null) {
navigationIcon.setColorFilter(whiteColor, PorterDuff.Mode.MULTIPLY);
toolbar.getMenu().findItem(R.id.share).setVisible(true);
if (actionBar != null) actionBar.setDisplayShowTitleEnabled(false);
// Reposition favorite fab
favoriteFabLayoutParams.setAnchorId(R.id.cart_fab_top_margin_view);
favoriteFabLayoutParams.anchorGravity = Gravity.CENTER_HORIZONTAL;
favoriteFab.setLayoutParams(favoriteFabLayoutParams);
favoriteFab.show();
}
else if (state == AppBarStateListener.COLLAPSED && navigationIcon != null) {
navigationIcon.setColorFilter(toolbarIconColor, PorterDuff.Mode.MULTIPLY);
toolbar.getMenu().findItem(R.id.share).setVisible(false);
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(productBrandTextView.getText().toString());
}
// Reposition favorite fab
favoriteFabLayoutParams.setAnchorId(R.id.cart_fab_left_margin_view);
favoriteFabLayoutParams.anchorGravity = Gravity.CENTER_VERTICAL;
favoriteFab.setLayoutParams(favoriteFabLayoutParams);
favoriteFab.show();
}
else {
favoriteFab.hide();
}
}
});
}
但是在布局加载过程中我遇到了这个错误 - 在布局完成之前,CoordinatorLayout测量开始后,锚点可能无法更改。
这有什么不妥?