2011-10-01 77 views
1

我想要实现简单的任务 - 在对话框关闭之前,我想根据我的逻辑设置不同的关闭动画(getWindow()。getAttributes()。windowAnimations = ...)。例如,我在对话框上有2个按钮,如果按下第一个按钮,我想向左滑动,如果按下第二个按钮,向右滑动。我创建了一些带有android:windowExitAnimation和android:windowEnterAnimation动画的样式文件,如果在自定义对话框构造函数中传递,它们将工作。但我不能覆盖代码中的windowAnimations作为构造函数的方法不能使用,因为我需要不同的动画。如何做到这一点,以及为什么这段代码无法正常工作?在运行时更改动画

 // close button 
     _button_close = (ImageButton)findViewById(R.id.buttonClose); 

     if (_button_close != null) 
     { 
      _button_close.setOnClickListener(
       new Button.OnClickListener() 
       { 
        public void onClick(View v) 
        { 
         // set animation 
         getWindow().getAttributes().windowAnimations = R.style.DialogSlideOutLeft; 

         // close form 
         dismiss(); 
        } 
       } 
      ); 
     } 

回答

1

我有同样的问题。 这里是我的解决方案:`

 alertCheckIn = new Dialog(this); 
     alertCheckIn.setTitle("Check-In"); 
     alertCheckIn.setContentView(textEntryView); //my custom dialog layout 

     lpDialog = alertCheckIn.getWindow().getAttributes(); 
     lpDialog.windowAnimations = R.style.FadeInDropOutDialogAnimation; 
     alertCheckIn.getWindow().setAttributes(lpDialog); 

     alertCheckIn.show(); 

`

,然后当我想切换动画: `

 lpDialog.windowAnimations = R.style.FadeInSlideLeftOutDialogAnimation; 
     alertCheckIn.getWindow().setAttributes(lpDialog); 
     alertCheckIn.cancel();` 

时:

private WindowManager.LayoutParams lpDialog; 
+1

没有任何影响为了我 – stoefln