【问题标题】:How to keep floating action button anchored to my view after unlocking the lock screen?解锁锁定屏幕后如何保持浮动操作按钮固定在我的视图中?
【发布时间】:2018-03-08 03:35:54
【问题描述】:

我有一个 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 及更高版本上,

有什么想法吗?

【问题讨论】:

    标签: android button android-activity lockscreen floating


    【解决方案1】:

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

    源代码:

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

    就是这样,

    希望它能帮助大家解决这个奇怪的问题。

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多