【问题标题】:Animate an AlertDialog's entrance and exit为 AlertDialog 的入口和出口设置动画
【发布时间】:2012-08-21 05:27:13
【问题描述】:

我必须在 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
  • 应用专为 API 15+ 构建

这里有一些相关的资源,我无法从中得到答案

【问题讨论】:

  • 如果您可以使活动滑入和滑出。将它用于 AlertDialog 只需将其主题更改为对话框..这是创建活动来代替对话框并使其作为对话框工作..它会简化你的问题..只是一个建议。
  • 我考虑过,但在运行时操作其内容太痛苦了。

标签: android animation android-alertdialog android-dialogfragment


【解决方案1】:

这是使用上述参考和代码的工作代码。

// 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 中)。

<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 文件夹中来使用任何其他自定义文件。

谢谢。

【讨论】:

  • 如果自定义动画文件,那么它应该是styles.xml中的@anim/slide_in_left
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2014-02-02
  • 2020-08-31
  • 2011-04-15
  • 2021-02-03
相关资源
最近更新 更多