先来看下效果图:

adnroid仿miui的dialog_android


当中show和dismiss的时候有动画效果。


原先试过使用PopupWindow来做,可是使用的时候不是那么舒服,毕竟不是dialog嘛。

所以这次尝试还是使用dialog来做 ,非常多地方是引用了系统源代码(源代码是最好的老师)


首先看CustomDialog.java的构造函数:

protected CustomDialog(Context context) {
this(context, R.style.CustomDialog);
}

protected CustomDialog(Context context, int theme) {
super(context, theme);
mAlert = new AlertController(context, this, getWindow());

Window window = getWindow();
window.setGravity(Gravity.BOTTOM); // 设置window的位置为底部
window.setWindowAnimations(R.style.AnimationDialog); // 设置window的动画
window.setBackgroundDrawableResource(android.R.color.transparent); // 设置window背景透明
}


 

艾玛,不太会描写叙述,直接上源代码


源代码中有点小问题,addView功能没有实现,在AlertController中加入下面代码就可以:

public void apply(AlertController dialog) {
if (mTitle != null) {
dialog.setTitle(mTitle);
}
if (mMessage != null) {
dialog.setMessage(mMessage);
}
if (mPositiveButtonText != null) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
mPositiveButtonListener, null);
}
if (mNegativeButtonText != null) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
mNegativeButtonListener, null);
}
// addView的实现
if (mView != null) {
if (mViewSpacingSpecified) {
dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,
mViewSpacingBottom);
} else {
dialog.setView(mView);
}
}