今天我在努力解决对话框动画的问题,最终通过样式使其工作,这里是一个示例。
首先,最重要的事情——今天我可能用了5种不同的方法让它工作了,但是因为......如果您设备的动画设置为“无动画”(设置→显示→动画),那么无论做什么,对话框都不会有动画效果!
以下是我的styles.xml的简化版本。希望它很清楚易懂。这应该位于res/values
目录中。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>
<style name="PauseDialogAnimation">
<item name="android:windowEnterAnimation">@anim/spin_in</item>
<item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>
</resources>
windowEnterAnimation
是我的动画之一,位于res\anim
中。
windowExitAnimation
是Android SDK中的动画之一。
然后,在我的活动的onCreateDialog(int id)
方法中创建对话框时,我执行以下操作。
Dialog dialog = new Dialog(this, R.style.PauseDialog)
// Setting the title and layout for the dialog
dialog.setTitle(R.string.pause_menu_label)
dialog.setContentView(R.layout.pause_menu)
或者,您可以通过以下方式设置动画,而不是使用带有主题的对话框构造函数。
Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;