为AlertDialog入口和出口设置动画

时间:2012-08-21 05:27:14

标签: android animation android-alertdialog android-dialogfragment

当它进入时我必须滑入AlertDialog并在它被解除时将其滑出, 但它不是动画。

那么如何让动画工作呢?

这就是我所拥有的,

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>

我已经提到了太多的资源,似乎没有一种正确的方法来做这件事对我有用,可能是我错过了什么

我正在使用

  • Android ICS
  • App专为API 15 +
  • 而构建

以下是一些我无法从

得到答案的相关资源

1 个答案:

答案 0 :(得分:0)

这是使用上面的参考文献和代码的工作代码。

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

// You can even inflate a layout to the AlertDialog.Builder, if looking for 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 follow.
if (dialog.getWindow() != null)
    dialog.getWindow().getAttributes().windowAnimations = R.style.SlidingDialogAnimation;

dialog.show();

关于样式,我使用与问题(在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文件夹中来使用任何其他自定义文件。

...

注意:- 如果您是SO用户,那么您只是对答案投下反对票而没有实施或测试,这很有趣,那么我为您感到抱歉。 对此有任何疑问或进一步的帮助,请在下面给我留言。

谢谢。

相关问题