美文网首页
Dialog相关

Dialog相关

作者: 主音King | 来源:发表于2020-09-30 15:50 被阅读0次

    DialogFragment显示回调:
    onCreate-->onCreateDialog-->onCreateView-->onViewCreated-->onResume
    Google推荐使用DialogFragment
    Activity销毁重建DialogFragment会自动重建,而Dialog不会。(横竖屏切换)

    自定义Dialog(android.app.Dialog)

    public class PermissionRequestDialog extends Dialog {
        private View.OnClickListener allowListener;
        private View.OnClickListener refuseListener;
        private Context mContext;
        private String title;
        private String message;
        private String allowName;
        private String refuseName;
    
        public PermissionRequestDialog(@NonNull Context context) {
            this(context, R.style.Dialog);
        }
    
        public PermissionRequestDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
            mContext = context;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_permission_request);
            Window window = getWindow();
            if (window != null) {
                Log.d(TAG, "onCreate: ");
                LayoutParams lp = window.getAttributes();
                lp.width = MATCH_PARENT;
                lp.height = MATCH_PARENT;
                window.setAttributes(lp);
                window.getDecorView().setPadding(0, 0, 0, 0);
                window.setGravity(Gravity.CENTER);
            }
            TextView tvMsg = findViewById(R.id.tvMsg);
            if (message != null) {
                tvMsg.setText(message);
            }
            TextView tvTitle = findViewById(R.id.tvTitle);
            if (title != null) {
                tvTitle.setText(title);
            }
            TextView tvRefuse = findViewById(R.id.tvRefuse);
            if (refuseName != null) {
                tvRefuse.setText(refuseName);
            }
            tvRefuse.setOnClickListener(v -> {
                if (refuseListener != null) {
                    refuseListener.onClick(v);
                }
                dismiss();
            });
            TextView tvAllow = findViewById(R.id.tvAllow);
            if (allowName != null) {
                tvAllow.setText(allowName);
            }
            tvAllow.setOnClickListener(v -> {
                if (allowListener != null) {
                    allowListener.onClick(v);
                }
                dismiss();
            });
    
            float sScale = mContext.getResources().getDisplayMetrics().density;
            int widthPx = mContext.getResources().getDisplayMetrics().widthPixels;
            int heightPx = mContext.getResources().getDisplayMetrics().heightPixels;
            LinearLayout dialogConstraint = findViewById(R.id.dialogConstraint);
            ViewGroup.LayoutParams params = dialogConstraint.getLayoutParams();
            if (params != null) {
                int widthDp = (int) (widthPx / sScale);
                int heightDp = (int) (heightPx / sScale);
                int widthPxDialog;
                if (widthPx > heightPx) {
                    // 横版
                    if (widthDp > 360) {
                        // 过大用260
                        widthPxDialog = (int) (360 * sScale);
                    } else {
                        // 宽度达不到360-->很少遇到
                        widthPxDialog = (int) ((widthDp - 20) * sScale);
                    }
                } else {
                    // 竖版
                    if (widthDp > 360) {
                        // 一般平板
                        widthPxDialog = (int) (360 * sScale);
                    } else {
                        // 一般手机
                        widthPxDialog = (int) ((widthDp - 60) * sScale);
                    }
                }
                Log.d(TAG, "onCreate: sScale:" + sScale + " widthPx:" + widthPx + " heightPx:" + heightPx +
                        " widthDp:" + widthDp + " heightDp:" + heightDp + " widthDialog:" + widthPxDialog);
                params.width = widthPxDialog;
                dialogConstraint.setLayoutParams(params);
            }
            setCancelable(false);
        }
    
        public PermissionRequestDialog setMessage(String message) {
            if (message != null) {
                this.message = message;
            }
            return this;
        }
    
        public PermissionRequestDialog setMessage(List<String> tips) {
            if (tips != null && !tips.isEmpty()) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < tips.size(); i++) {
                    if (i == tips.size() - 1) {
                        builder.append(tips.get(i));
                    } else {
                        builder.append(tips.get(i)).append("\n");
                    }
                }
                this.message = builder.toString();
            }
            return this;
        }
    
        public PermissionRequestDialog setTitle(String title) {
            this.title = title;
            return this;
        }
    
        public PermissionRequestDialog setAllowListener(View.OnClickListener allowListener) {
            this.allowListener = allowListener;
            return this;
        }
    
        public PermissionRequestDialog setAllowListener(String name, View.OnClickListener allowListener) {
            allowName = name;
            this.allowListener = allowListener;
            return this;
        }
    
        public PermissionRequestDialog setRefuseListener(View.OnClickListener refuseListener) {
            this.refuseListener = refuseListener;
            return this;
        }
    
        public PermissionRequestDialog setRefuseListener(String name,View.OnClickListener refuseListener) {
            refuseName = name;
            this.refuseListener = refuseListener;
            return this;
        }
    
        @Override
        public void show() {
            Log.d(TAG, "show: ");
            Window window = getWindow();
            if (window != null) {
                window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
                View decor = window.getDecorView();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
                } else {
                    decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                }
                super.show();
                window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            } else {
                super.show();
            }
        }
    }
    
    

    样式:

       <style name="Dialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">#00000000</item>
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:background">@null</item>
            <item name="android:backgroundDimEnabled">true</item>
        </style>
    

    注意点:Dialog的布局Window->DecorView-->FrameLayout-->xml布局
    为什么系统dialog不充满全屏
    充满全屏:
    LayoutParams lp = window.getAttributes();
    lp.width = MATCH_PARENT;
    lp.height = MATCH_PARENT;
    window.setAttributes(lp);
    控制paddiing:
    window.getDecorView().setPadding(0, 0, 0, 0);
    重写show方法:防止底部导航隐藏闪烁(如华为手机等)

    相关文章

      网友评论

          本文标题:Dialog相关

          本文链接:https://www.haomeiwen.com/subject/bheduktx.html