Using WindowManager to dynamically change the height and width of dialong in android

Keywords: Mobile xml

In recent projects, it has been found that in Huawei mobile phones or some full screen mobile phones, dialog pop-up boxes may have various adaptation problems. Using WindowManager to dynamically modify dialog can perfectly solve this problem

//Dialog XML layout
final View vv = View.inflate(context, R.layout.pay, null);
                TextView finalPay = vv.findViewById(R.id.finalPay);
                    final Dialog alertDialog = new Dialog(context);
                    alertDialog.setContentView(vv);
                    // Get the Dialog Window and modify the Window properties
                    // Get the Dialog Window and modify the Window properties
                    Window window = alertDialog.getWindow();
                    //Setting the padding of dialog
                    window.getDecorView().setPadding(50, 0, 50, 0);
                    window.getDecorView().setBackgroundColor(Color.TRANSPARENT);
                    // Get LayoutParams of Window
                    WindowManager.LayoutParams attributes = window.getAttributes();
                    attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                    attributes.gravity = Gravity.CENTER | Gravity.CENTER_HORIZONTAL;
                    // It must be reset to take effect
                    window.setAttributes(attributes);
                    alertDialog.show();
            
                //Set the operation in write dialog
                vv.findViewById(R.id.pay_close).setOnClickListener(new 
                      View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });

Most of the adaptation problems can be solved by writing here

Later, in a highly adaptive dialog, there was a problem that the height was intercepted

At one time, I thought it was because of the xml layout problem. I checked that the height was already wrap [content], but it would still appear. Later, I added

 attributes.height=WindowManager.LayoutParams.MATCH_PARENT;
With the height set to match ﹣ parent ﹣ it perfectly solves the problem of self adaption (I always wonder if the match ﹣ parent here is the height of wrap ﹣ content in the layout? Waiting for the big God to answer)

 

 

Posted by fizzystutter on Sat, 23 Nov 2019 10:46:00 -0800