【问题标题】:Disabling (fade in and fade out) dialog box animation禁用(淡入淡出)对话框动画
【发布时间】:2013-08-09 19:41:41
【问题描述】:

我是新手程序员,在禁用对话框动画(淡入淡出)时遇到问题。

我尝试使用空样式并通过更改来设置它

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

进入

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

对话框背景变黑,正负按钮改成

我的风格:

<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 来代替进入和退出动画?
  • &lt;item name="android:windowAnimationStyle"&gt;@null&lt;/item&gt;
  • 与@anim/enter 和@anim/exit 的效果完全相同。我的意思是 @null@null@null

标签: android dialog


【解决方案1】:

终于成功了!

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);
}

【讨论】:

  • 我对这个解决方案的唯一问题是当您重新进入应用程序然后关闭对话框时。我的解决方案是使用 getDialog().getWindow().setWindowAnimations(R.style.DialogWithAnimation);在 onResume 中延迟发布
【解决方案2】:

这是一个简单的解决方案:

在你的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();

享受

【讨论】:

    猜你喜欢
    • 2017-03-13
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2012-12-18
    • 2021-10-17
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多