【问题标题】:why is my FAB animation going out of screen with Material BottomAppBar?为什么我的 FAB 动画使用 Material BottomAppBar 超出屏幕?
【发布时间】:2021-01-03 18:06:14
【问题描述】:

所以我得到了这个工作,它很棒

然后我尝试将父 FAB 设置在右侧:

binding.bottomAppBar.fabAlignmentMode = BottomAppBar.FAB_ALIGNMENT_MODE_END

它可以工作,但现在父 FAB 动画消失了。

这里可以更好地了解正在发生的事情:

打开动画:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="45"
    android:duration="300" />

关闭动画:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
    android:fromDegrees="45"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="0"
    android:duration="300"
    />

活动 XML:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_gravity="bottom"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fabAlignmentMode="end"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addFav"
        app:layout_anchor="@id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我该如何解决这个问题?子 FAB 动画按预期工作。

【问题讨论】:

    标签: android kotlin material-design


    【解决方案1】:

    这就是正在发生的事情。

    1. 当 XML 膨胀时,FAB 位于中心。所以pivotX 现在是屏幕的中心。
    2. 您在膨胀后以编程方式在 Java/Kotlin 中设置 fabAlignmentMode
    3. 当轮换开始时,pivotX 仍在使用膨胀点的值,因此使用旧值。
    4. FAB 围绕其先前位置的中心旋转,因此离开屏幕。请注意第二个 GIF 中 FAB 的运动曲线。

    使用fabAlignmentMode 在 XML 中设置对齐方式应该可以解决您的问题。

    【讨论】:

    • 刚刚在一个新项目中进行了测试以确保它没有工作,如果你想查看它,我从下面添加了一个新的 GIF。
    • @Joaco 您是否在 FAB 上设置了与位置相关的任何属性?
    • 不,我也添加了xml。
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多