弹出位置–>>> 屏幕中间
1.通过代码设置Dialog
//获取当前布局的Window Window window = getWindow(); //设置无标题栏 window.requestFeature(Window.FEATURE_NO_TITLE); //背景为透明 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //设置Dialog的布局 setContentView(R.layout.dialog_common); WindowManager.LayoutParams params = window.getAttributes(); //显示隐藏的动画效果 params.windowAnimations = R.style.MyDialogAnimationCenter; params.gravity = Gravity.CENTER; //位置在屏幕处于居中位置
2.设置动画效果
<!-- Dialog从center弹出的动画效果--> <style name="MyDialogAnimationCenter" > <item name="android:windowEnterAnimation">@anim/dialog_bottom_center_enter</item> <item name="android:windowExitAnimation">@anim/dialog_bottom_center_exit</item> </style>
3.设置动画属性(创建于res\anim)
--->> dialog_bottom_center_enter.xml文件 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="-200%" android:toYDelta="0"/> <alpha android:duration="200" android:fromAlpha="0" android:toAlpha="1" android:interpolator="@android:interpolator/linear"/> </set> --->>dialog_bottom_center_exit.xml文件 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="0%" android:toYDelta="200%"/> <alpha android:duration="300" android:fromAlpha="1" android:toAlpha="0" android:interpolator="@android:interpolator/linear"/> </set>
弹出位置–>>> 屏幕底部
1.通过代码设置Dialog
//获取当前布局的Window Window window = getWindow(); //设置无标题栏 window.requestFeature(Window.FEATURE_NO_TITLE); //背景为透明 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置Dialog的布局 setContentView(R.layout.dialog_usercenter_edit_sex); WindowManager.LayoutParams params = window.getAttributes(); //位置在屏幕处于底部位置 params.gravity = Gravity.BOTTOM; //显示隐藏的动画效果 params.windowAnimations = R.style.MyDialogAnimation;
2.设置动画效果(创建于values\styles文件)
<!-- Dialog从底部弹出的动画效果--> <style name="MyDialogAnimation" > <item name="android:windowEnterAnimation">@anim/dialog_bottom_center_enter</item> <item name="android:windowExitAnimation">@anim/dialog_bottom_center_exit</item> </style>
3.设置动画属性
--->> dialog_bottom_enter.xml文件 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromYDelta="100%" android:toYDelta="0" > </translate> --->> dialog_bottom_exit.xml文件 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromYDelta="0" android:toYDelta="100%" > </translate>