禁用(淡入淡出)对话框动画

11

我是一名初级程序员,我在禁用对话框动画(淡入淡出)方面遇到了问题。

我尝试使用空的样式,并通过更改它来设置,但是它并没有起作用。


final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

进入

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.NoAnimation));

对话框的背景变成了黑色,积极和消极按钮采用了2.1至4.0版本安卓的样式,但淡入淡出的动画效果仍保留...

我的风格:

<style name="DialogNoAnimation">
    <item name="android:windowEnterAnimation">@anim/enter</item>
    <item name="android:windowExitAnimation">@anim/exit</item>
</style>

<style name="NoAnimation" parent="@android:style/Theme.Dialog">
    <item name="android:windowAnimationStyle">@style/DialogNoAnimation</item>
</style>

你有什么想法可以消除这个动画吗?


你尝试过在进入和退出动画中添加@null吗? - Nikola Despotoski
or <item name="android:windowAnimationStyle">@null</item> - Nikola Despotoski
与@anim/enter和@anim/exit完全相同的效果。我的意思是使用<item name="android:windowEnterAnimation">@null</item>和<item name="android:windowExitAnimation">@null</item>或<item name="android:windowAnimationStyle">@null</item>。 - Stramek
2个回答

13

终于成功了!

res/anim/enter.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="@android:integer/config_shortAnimTime"/>

res/anim/exit.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_shortAnimTime"/>

res/values/styles.xml

<style name="DialogNoAnimation">
    <item name="android:windowEnterAnimation">@anim/enter</item>
    <item name="android:windowExitAnimation">@anim/exit</item>
</style>

src/[dialog_box_class].java

@Override
public void onStart()
{
  super.onStart();
  if (getDialog() == null)
    return;
  getDialog().getWindow().setWindowAnimations(R.style.DialogNoAnimation);
}

我在这个解决方案中遇到的唯一问题是当您重新进入应用程序然后关闭对话框时。我的解决方案是在onResume中使用getDialog().getWindow().setWindowAnimations(R.style.DialogWithAnimation); 并进行延迟发布。 - Anton Makov
@AntonMakov,当您重新进入时发生了什么? - c-an

10

这里有一个简单的解决方案:

在你的styles.xml中定义一个自定义样式:

<style name="Dialog">
  <item name="android:windowAnimationStyle">@null</item>
  //... more items
</style>

使用自定义样式创建新的构建器:

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Dialog);
builder.setTitle("Dialog title");
builder.show();

享受


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接