如何在解锁锁定屏幕后将浮动动作按钮固定在我的视图中?
问题描述:
我有一个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观点:
但是,如果屏幕被锁定和解锁回来了本次活动运行,我在这几秒钟:
此问题发生在Android 4.2及以上版本,
有些想法吗?
答
我最终找到了一个解决方案,只需将FloatingActionButton的可见性设置为在activity的onPause()方法中完成,然后在activity的onResume()方法中将其可见性设置为VISIBLE,对于这种情况非常适用, FloatingActionButton恢复其挂靠性质再次显示时,
的源代码:
@Override
protected void onResume() {
fab.setVisibility(View.VISIBLE);
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
fab.setVisibility(View.GONE);
}
That's所有,
希望它可以帮助大家用这个奇怪的问题。