【问题标题】:Navigation Architecture Component: transition animations not working for dialog导航架构组件:过渡动画不适用于对话框
【发布时间】:2019-12-19 03:11:56
【问题描述】:

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

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

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

这是我导航图中的相关 sn-p:

<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 很遗憾目前还没有

标签: android android-architecture-navigation


【解决方案1】:

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>
  1. 在DialogFragment里面设置样式为windowAnimations
     override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
    }

查找更多here

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2021-09-27
    • 2021-08-09
    • 2021-11-22
    相关资源
    最近更新 更多