导航架构组件:过渡动画不适用于对话框

Pse*_*328 12 android android-architecture-navigation

<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>
Run Code Online (Sandbox Code Playgroud)

这是输入动画:

<?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>
Run Code Online (Sandbox Code Playgroud)

这是退出动画:

<?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>
Run Code Online (Sandbox Code Playgroud)

zoh*_*131 16

从版本开始,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>
Run Code Online (Sandbox Code Playgroud)
  1. 在 DialogFragment 中将样式设置为 windowAnimations
     override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
    }
Run Code Online (Sandbox Code Playgroud)

在此处查找更多信息