解锁锁定屏幕后如何将浮动操作按钮固定在我的视图中?

How to keep floating action button anchored to my view after unlocking the lock screen?

我有一个 FloatingButton 锚定到 CoordinatorLayout 中的视图,一切正常,但如果我锁定屏幕并解锁,FloatinButton 将失去其锚定 属性 并在底部浮动一段时间。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.user.myapplication.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_scrolling" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

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

如您所见,floatingButton 锚定到 app_bar 视图:

但是,如果使用此 activity 运行 锁定和解锁屏幕,我将等待几秒钟:

此问题发生在 Android 4.2 及更高版本,

有什么想法吗?

我终于找到了解决方案,只需在 activity 的 onPause() 方法中将 FloatingActionButton 的可见性设置为 GONE,然后在 activity 的 onResume() 中将其可见性设置为 VISIBLE方法,在这种情况下效果很好,因为 FloatingActionButton 在再次显示时恢复其锚定属性,

源代码:

    @Override
    protected void onResume() {
        fab.setVisibility(View.VISIBLE);
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        fab.setVisibility(View.GONE);
    }

就这些了,

希望对大家解决这个奇怪的问题有所帮助。