对AlertDialog的入口和出口进行动画处理 [英] Animate an AlertDialog's entrance and exit

查看:177
本文介绍了对AlertDialog的入口和出口进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 AlertDialog 进入时滑动,而在它被关闭时滑出它,
但它没有动画。

I have to slide in an AlertDialog when it enters and slide it out when it is dismissed, but it is not animating.

那么我如何使动画起作用?

So how do I get the animation to work ?

这里是我所拥有的,

public class SlideDialogFragment extends DialogFragment {
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
              return  new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),R.style.SlidingDialog))
                      .setTitle("Sliding dialog")
                      .create()
     }

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="SlidingDialog" parent="@android:style/Theme.DeviceDefault.Dialog">
        <item name="android:windowAnimationStyle">@style/SlidingDialogAnimation</item>
    </style>
    <style name="SlidingDialogAnimation">
        <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
        <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
    </style>
</resources>

我提到了太多资源,似乎没有一个合适的方法对我有用的,也许是我丢失了一些东西

I have referred too many resources and there doesn't seem to be a single proper way to do this that works for me, may be I am missing something

我正在使用


  • Android ICS

  • 应用程序是为API 15+构建的

以下是一些相关资源我无法从

Here are some related resources that I couldn't get the answer from

  • Animate a dialog fragment on dismiss
  • https://groups.google.com/d/msg/android-developers/0oCWqQC4Pww/CmUM7iNHUggJ
  • https://groups.google.com/d/msg/android-developers/a2pUV0Sigf4/WiJNg_vMQWwJ
  • https://stackoverflow.com/a/3870997/492561

推荐答案

// Declare a Builder.
AlertDialog.Builder builder = new AlertDialog.Builder(context);

// You can even inflate a layout to the AlertDialog.Builder, if looking to create a custom one.
// Add and fill all required builder methods, as per your need.


// Now create object of AlertDialog from the Builder.
final AlertDialog dialog = builder.create();

// Let's start with animation work. We just need to create a style and use it here as follows.
if (dialog.getWindow() != null)
    dialog.getWindow().getAttributes().windowAnimations = R.style.SlidingDialogAnimation;

dialog.show();

关于样式,我使用了与问题中相同的样式(在styles.xml中)。

Regarding Style, I used the same style as used in question (in styles.xml).

<style name="SlidingDialogAnimation">
        <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
        <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>

但是您可以通过将动画XML文件放置在res / anim文件夹中来使用任何其他自定义文件。

But you can use any other custom files by placing an animation XML file in the res/anim folder.

谢谢。

这篇关于对AlertDialog的入口和出口进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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