DialogFragment 设置进出动画和宽高

在onCreatView中 直接设置 getDialog().getWindow().setWindowAnimations(R.style.animate_dialog);

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


    



enter:

    


out:


    



在Activity中创建dialogfragment时,指定动画时会报错:

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



你可能感兴趣的:(动画)