自定义Dialog,实现由下而上的弹出效果(模仿QQ退出等)

时间:2022-03-07 14:32:56

方法:

public Dialog createDialog(Context context, View view) {
Dialog mSelectPhotoDialog = null;
mSelectPhotoDialog = new Dialog(context, R.style.transparentFrameWindowStyle);
mSelectPhotoDialog.setContentView(view);
Window window = mSelectPhotoDialog.getWindow();
// 设置显示动画
window.setWindowAnimations(R.style.select_photo_dialog_animstyle);
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;
wl.y = getActivity().getWindowManager().getDefaultDisplay().getHeight();
// 以下这两句是为了保证按钮可以水平满屏
wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
// 设置显示位置
mSelectPhotoDialog.onWindowAttributesChanged(wl);
// 设置点击外围解散
mSelectPhotoDialog.setCanceledOnTouchOutside(true); mSelectPhotoDialog.show();
}

样式:

<!-- 头像弹出窗的动画效果 -->
<style name="select_photo_dialog_animstyle">
<item name="android:windowEnterAnimation">@anim/photo_dialog_in_anim</item>
<item name="android:windowExitAnimation">@anim/photo_dialog_out_anim</item>
</style>
<!-- 头像弹出窗的样式:无边框 背景透明-->
<style name="transparentFrameWindowStyle" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
动画:
(一)、photo_dialog_in_anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="1000"
android:toXDelta="0"
android:toYDelta="0" /> </set>

(二)、photo_dialog_out_anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="1000" /> </set>