导航架构组件:过渡动画不适用于对话框 [英] Navigation Architecture Component: transition animations not working for dialog

查看:18
本文介绍了导航架构组件:过渡动画不适用于对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的导航图中有一个 带有进入/退出动画,但动画不适用于对话框.我已经在 <fragment 节点上测试了它们,并且它们工作正常.

I have a <dialog in my nav graph with enter/exit anims but the animations aren't working for the dialog. I've tested them on <fragment nodes and those work fine.

为了澄清起见,被引用的对话框是一个 DialogFragment

For clarification, the dialog being referenced is a DialogFragment

这是限制还是我做错了什么?

Is this a limitation or am I doing something wrong?

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

Here's the relevant snippet from my nav graph:

<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>

推荐答案

2.2.0-alpha02 版本开始,这是 Navigation 组件的限制.可以查看DialogFragmentNavigator

As of version 2.2.0-alpha02 this is the limitation of Navigation component. You can view the source code of DialogFragmentNavigator

但是,您可以使用以下步骤轻松实现 DialogFragment 的动画:

However, you can pretty easily achieve animation for DialogFragment using the following steps:

  1. anim 文件夹中创建一个提及进入和退出动画的样式:
  1. Create a style mentioning the enter and exit animations from anim folder:

    <style name="MyDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/enter_anim</item>
        <item name="android:windowExitAnimation">@anim/exit_anim</item>
    </style>

  1. 在 DialogFragment 中将样式设置为 windowAnimations

     override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
    }

此处了解更多信息.

这篇关于导航架构组件:过渡动画不适用于对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆