导航架构组件:对话框的过渡动画无法工作

18

我的导航图中有一个带有进入/退出动画的<dialog,但是这些动画对话框不起作用。我已经在<fragment节点上测试过它们,那里工作正常。

为了澄清,所引用的对话框是DialogFragment

这是我的导航图中相关代码片段:

<fragment
        android:id="@+id/fragment_home"
        android:name="com.my.project.fragments.HomeFragment"
        android:label="@string/nav_home"
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_fragment_home_to_fragment_dialog_new_user_welcome"
            app:destination="@id/fragment_dialog_new_user_welcome"
            app:enterAnim="@anim/nav_fade_enter_anim"
            app:exitAnim="@anim/nav_fade_exit_anim"
            app:popUpTo="@layout/fragment_home" />
    </fragment>

    <dialog
        android:id="@+id/fragment_dialog_new_user_welcome"
        android:name="com.my.project.fragments.NewUserWelcomeDialog"
        tools:layout="@layout/fragment_dialog_new_user_welcome">

        <action
            android:id="@+id/action_fragment_dialog_new_user_welcome_to_activity_discover_detail"
            app:destination="@id/fragment_discover_detail"
            app:launchSingleTop="true"
            app:popUpTo="@id/fragment_home" />
    </dialog>

这里是进入动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

这是退出动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

你好,你找到解决方案了吗? - Yosef
抱歉,目前还没有。 - Psest328
1个回答

23
截至版本2.2.0-alpha02,这是导航组件的限制。您可以查看DialogFragmentNavigator的源代码。
但是,您可以通过以下步骤轻松实现DialogFragment动画:
  1. 创建一个样式,其中包含来自anim文件夹的进入和退出动画:
    <style name="MyDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/enter_anim</item>
        <item name="android:windowExitAnimation">@anim/exit_anim</item>
    </style>

在DialogFragment中设置样式为windowAnimations
     override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
    }

在这里找到更多信息


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