如果根布局的android:animateLayoutChanges属性为true,则Snackbar会闪烁

9
似乎当android:animateLayoutChanges设置为true时,一个简单的Snackbar消息看起来相当突兀。Snackbar将在动画期间反复闪烁。从布局中删除android:animateLayoutChanges参数可以解决此问题,但现在我将无法享受其带来的好处。如果我在子视图而不是根视图上使用android:animateLayoutChanges,那么它也会起作用。
这是否是已知问题?是否有办法解决?
下面是一个示例布局,如果显示Snackbar,则会显示该问题。
<?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:animateLayoutChanges="true">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="end"
        app:fabCradleMargin="4dp"
        app:fabCradleRoundedCornerRadius="16dp"
        app:fabCradleVerticalOffset="4dp"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
        app:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/colorAccent"
        app:layout_anchor="@id/bottomAppBar"
        app:srcCompat="@drawable/ic_add"
        app:tint="@color/colorWhite" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
2个回答

2

看起来是一个未解决的 bug。请查看这里


1

我也遇到了同样的问题,截至2021年7月30日仍未解决。

如果适用的话,最简单的解决方法是将需要动画的布局部分放在额外的FrameLayout中,例如像这样:

<androidx.coordinatorlayout.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:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity"
    android:theme="@style/AppTheme"
    
    <-- Other views here, FragmentContainerView, AppBarLayout, ... -->

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">
    
        <-- Place anything that you want animated here -->

    </FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果您在“无用”的FrameLayout中有其他容器视图,可能会在Android Studio中收到“UselessParent”提示,您可以在添加的FrameLayout的子项中包含此忽略:tools:ignore="UselessParent"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接