1. 程式人生 > >android 小米手機彈出框(自定義dialog)位置不居中解決方法

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 在佈局中的位置
            }
        }