</pre>在onCreatView中 直接设置 getDialog().getWindow().setWindowAnimations(R.style.animate_dialog);<p></p><p></p><pre code_snippet_id="561426" snippet_file_name="blog_20141224_2_9769632" name="code" class="java">public class AnimateDialog extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //隐藏title getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //设置dialog的 进出 动画 getDialog().getWindow().setWindowAnimations(R.style.animate_dialog); View view=inflater.inflate(R.layout.layout_dialog, null); return view; } }
<style name="animate_dialog"> <item name="android:windowEnterAnimation">@anim/dialog_enter</item> <item name="android:windowExitAnimation">@anim/dialog_out</item> </style>
<translate android:fromYDelta="100%p" android:toYDelta="0%p" android:duration="500" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>out:
<translate android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="500" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
AnimateDialog animateDialog=new AnimateDialog(); //会报null,不能这样设置dialogfragment的进出动画 Window window=animateDialog.getDialog().getWindow(); window.setWindowAnimations(R.style.animate_dialog);
设置DialogFragment 的宽高:
在 onCreatView中设置是没有效果的,需要在onStart方法中设置,如下
public class AnimateDialog extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //隐藏title getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //设置dialog的 进出 动画 getDialog().getWindow().setWindowAnimations(R.style.animate_dialog); /*写在这里没效果,要写在onStart里*/ // getDialog().getWindow().getAttributes().width=getResources().getDisplayMetrics().widthPixels-200; View view=inflater.inflate(R.layout.layout_dialog, null); return view; } @Override public void onStart() { /*设置对话框的宽高*/ getDialog().getWindow().getAttributes().width=getResources().getDisplayMetrics().widthPixels-200; /*下面的方式设置也行*/ // getDialog().getWindow().setLayout(getResources().getDisplayMetrics().widthPixels-200, getDialog().getWindow().getAttributes().height); super.onStart(); } }