API 26调用setAppearanceLightStatusBars后CollapsingToolbarLayout滚动异常
Android API 26及以下版本中,
setAppearanceLightStatusBars导致CollapsingToolbarLayout滚动异常 初始正常状态
通过Android模板创建的ScrollingActivity,初始滚动功能正常,布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.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=".ScrollingActivity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height" android:fitsSystemWindows="true" android:theme="@style/Theme.TestApplication.AppBarOverlay"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/toolbar_layout" style="@style/Widget.MaterialComponents.Toolbar.Primary" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:toolbarId="@+id/toolbar"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/Theme.TestApplication.PopupOverlay" /> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <include layout="@layout/content_scrolling" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/fab_margin" app:layout_anchor="@id/app_bar" app:layout_anchorGravity="bottom|end" app:srcCompat="@android:drawable/ic_dialog_email" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>

异常触发场景
在AppBarLayout的偏移监听中调用setAppearanceLightStatusBars切换状态栏样式后,CollapsingToolbarLayout滚动功能出现异常,代码如下:
binding.appBar.addOnOffsetChangedListener { appbarLayout, verticalOffset: Int -> val seekPosition = -verticalOffset / appbarLayout.totalScrollRange.toFloat() val isLightStatusBar = seekPosition > 0.5 if (isLightStatusBar != WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars) { WindowCompat.getInsetsController( window, window.decorView ).isAppearanceLightStatusBars = isLightStatusBar } }

问题特性
- 仅在Android API 26及以下版本出现,Android新版本无此问题
- 使用已废弃的
SYSTEM_UI_FLAG_LIGHT_STATUS_BAR设置浅色状态栏时,不会触发该滚动异常
内容的提问来源于stack exchange,提问作者Linh
相关产品推荐
相关产品推荐

