天天看点

android中Dialog居中显示

//dialog 居中显示
                Window window = dialog.getWindow();
                if (dialog != null && window != null) {
                    WindowManager.LayoutParams attr = window.getAttributes();
                    if (attr != null) {
                        attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                        attr.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                        attr.gravity = Gravity.CENTER;//设置dialog 在布局中的位置
                    }
                }