您的位置:首页 > 移动开发 > Android开发

android自定义dialog窗体的大小和位置

2014-12-14 09:47 405 查看
不设置dialog窗体的位置和大小:

View dialogView = getView(context, R.layout.dialog_view);
final Dialog mAlertDialog = new Dialog(context, R.style.theme_dialog);
mAlertDialog.setContentView(dialogView);
mAlertDialog.show();


效果如下:



通过获取屏幕的像素,按比例设置弹出dialog窗体的大小和位置:

View dialogView = getView(context, R.layout.dialog_view);

final Dialog mAlertDialog = new Dialog(context, R.style.theme_dialog);
mAlertDialog.setContentView(dialogView);

//获取屏幕宽高
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width =display.getWidth();
int height=display.getHeight();

Window dialogWindow = mAlertDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
//lp.x与lp.y表示相对于原始位置的偏移.
//将对话框的大小按屏幕大小的百分比设置
lp.x = (int) (width*0.05); // 新位置X坐标
lp.y = (int) (height*0.2); // 新位置Y坐标
lp.width = (int) (width*0.9); // 宽度
lp.height = (int) (height*0.6); // 高度

dialogWindow.setAttributes(lp);

mAlertDialog.show();
最终效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: