自定义对话框不显示视图

Custom Dialog not showing View

我做了一个简单的项目,如果出现按钮单击自定义对话框,但当我觉得其中缺少某些东西时? 当我单击按钮时出现 Toast 消息但对话框未显示,也没有发生错误并且应用程序没有崩溃。 源代码

private void layout() {
        toast("save");

        LayoutInflater dialoglayout = LayoutInflater.from(EditImageActivity.this);
         View dialogView = dialoglayout.inflate(R.layout.save_image_dialoge,null);

         my_dialog = new Dialog(EditImageActivity.this, R.style.CustomAlertDialog);
         my_dialog.setContentView(dialogView);
         WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
         lp.copyFrom(my_dialog.getWindow().getAttributes());
         lp.width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
         lp.height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
         my_dialog.getWindow().setAttributes(lp);
         final Button cancel = dialogView.findViewById(R.id.cancel);
         cancel.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 //cancel(true);
                 my_dialog.dismiss();
             }
         });
     }

您忘记调用 my_dialog.show() 来显示您的 my_dialog

private void layout() {
    toast("save");

    LayoutInflater dialoglayout = LayoutInflater.from(EditImageActivity.this);
     View dialogView = dialoglayout.inflate(R.layout.save_image_dialoge,null);

     my_dialog = new Dialog(EditImageActivity.this, R.style.CustomAlertDialog);
     my_dialog.setContentView(dialogView);
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
     lp.copyFrom(my_dialog.getWindow().getAttributes());
     lp.width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
     lp.height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
     my_dialog.getWindow().setAttributes(lp);
     final Button cancel = dialogView.findViewById(R.id.cancel);
     cancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             //cancel(true);
             my_dialog.dismiss();
         }
     });
     my_dialog.show();
 }

也调用这个方法

 my_dialog.show();

你的方法是这样的

private void layout() {
        toast("save");

        LayoutInflater dialoglayout = LayoutInflater.from(EditImageActivity.this);
         View dialogView = dialoglayout.inflate(R.layout.save_image_dialoge,null);

         my_dialog = new Dialog(EditImageActivity.this, R.style.CustomAlertDialog);
         my_dialog.setContentView(dialogView);
         WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
         lp.copyFrom(my_dialog.getWindow().getAttributes());
         lp.width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
         lp.height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);
         my_dialog.getWindow().setAttributes(lp);
         final Button cancel = dialogView.findViewById(R.id.cancel);
         cancel.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 //cancel(true);
                 my_dialog.dismiss();
             }
         });

       my_dialog.show();
     }