工作中经常要使用dialog,写的过程需要注意:大小,位置:
private void showDialog(View view, int padding) {
if(mDialog == null) {
mDialog = new Dialog(mContext);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
mDialog.setContentView(view);
Window dw = mDialog.getWindow();
WindowManager.LayoutParams lp = dw.getAttributes();
int dpx = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getWidth();
int dpy = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getHeight();
int iconSize = getResources().getDimensionPixelSize(R.dimen.status_bar_icon_size_big);
int[] location = new int[2];
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
getLocationOnScreen(location);
lp.x = location[0] - dpx / 2 + iconSize - iconSize / DIALOG_OFFSET_PART;
lp.y = location[1] - dpy / 2 - view.getMeasuredHeight() + DIALOG_OFFSET_DIMENSIONS
- padding;
lp.width = LayoutParams.WRAP_CONTENT;
lp.height = LayoutParams.WRAP_CONTENT;
dw.setAttributes(lp);
mDialog.show();
}
通过修改该方法来满足需求,屡试不爽