Android - DialogFragment 不显示进入动画

Android - DialogFragment Doesn't Display The Entering Animation

我正在使用导航组件导航到“DeathDialogFragment”,我希望它显示我的进入动画。

但不仅 XML UI 宽度和高度属性被完全忽略,因此我不得不在 onResume 上做一些解决方法,这导致对话框后面出现意外的黑框,您将在下面看到(多亏了 Android SDK) 但动画根本不会发生。 它只是显示为一个普通的警报对话框。

我尝试通过将我的进入动画对话框主题添加到 styles.xml 并在对话框的代码中实现它来在没有导航组件的情况下对其进行初始化。 当这不起作用时,我覆盖了 onViewCreated 而不是 onDialogCreated,结果仍然没有变化。

有谁知道为什么动画不起作用或如何删除无意的背景黑框?



对话框截图(不显示动画就这样弹出)

导航组件:



DeathDialogFragment

class DeathDialogFragment : DaggerDialogFragment()
{

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog
    {

        return if (activity != null && context != null)
        {
            val view: View = requireActivity().layoutInflater
                .inflate(R.layout.dialog_fragment_death, ConstraintLayout(requireActivity()), false)
            val dialog = AlertDialog.Builder(requireContext())
            dialog.setView(view)
            dialog.create()
        }
        else
        {
            Timber.e("getContext() returned NULL, cannot return the Dialog")
            super.onCreateDialog(savedInstanceState)
        }
    }

    override fun onResume()
    {
        super.onResume()
        val window = dialog!!.window ?: return
        val params = window.attributes
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window.attributes = params
    }

}



从 GameDialogFragment 导航到 DeathDialogFragment

Navigation.findNavController(binding.root).navigate(GameFragmentDirections.actionGameFragmentToDeathDialogFragment(score))
  • 返回普通对话框而不是警告对话框已解决问题:

DeathDialogFragment 的 onCreateDialog

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog
        {
           
         val dialog = Dialog(requireContext(), R.style.Dialog)
         dialog.setContentView(R.layout.dialog_fragment_death)
         dialog.findViewById<Button>(R.id.play_again_btn).setOnClickListener 
            {
                (requireArguments().get("PlayAgainListener") as PlayAgainListener).playAgain()
            }
         dialog.create()

         return dialog
        }



  • 从对话框布局的外部布局中删除大小和边距修改解决了意外的背景框问题。
    不要将边距添加到外部布局,而是将它们添加到视图中,当您在 onResume 上将 window 高度和宽度属性修改为 WRAP_CONTENT 时将产生相同的效果:

DeathDialogFragment 的 onResume:

override fun onResume()
{
    super.onResume()
    val window = dialog!!.window ?: return
    val params = window.attributes
    params.width = ViewGroup.LayoutParams.WRAP_CONTENT
    params.height = ViewGroup.LayoutParams.WRAP_CONTENT
    window.attributes = params
}

(dialog_fragment_death 具有我创建的自定义对话框样式,其边距未在问题中共享)