AlertDialog requestFeature() must be called before adding content问题

错误提示

 android.util.AndroidRuntimeException: requestFeature() must be called before adding content
 at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:358)
 at com.android.internal.app.AlertController.installContent(AlertController.java:240)

解决方式:

  1. alert.setCentView(xx); alertDialog.getWindow();放到alertDialog.show();后边

你可以在之前 AlertDialog.Builder.setView(xx);


我的代码

 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ModuleMagazineDetailActivity.this, R.style.MyAlertDialog);


 alertDialogBuilder.setView(view);
 final AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.show();

 alertDialog.getWindow().setDimAmount(0f);
 Window window = alertDialog.getWindow();
 window.setGravity(Gravity.BOTTOM);
 window.getDecorView().setPadding(0, 0, 0, 0);
 WindowManager.LayoutParams lp = window.getAttributes();
 lp.width = WindowManager.LayoutParams.MATCH_PARENT;
 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
 window.setAttributes(lp);

MyAlertDialog style代码

  

这里window + style的操作

  1. alertDialog 宽度等于屏幕宽度,
  2. 布局紧挨着软键盘或者是布局底部

问题2: alertDialog 点击外部dialog不消失

解决方式:

alertDialogBuilder.create() 后边加上

 alertDialog.setCancelable(true);
 alertDialog.setCanceledOnTouchOutside(true);

因为某些手机你不设置,可能会默认是false吧,比如vivo x7这种神机。


你可能感兴趣的:(alertDialog,dialog宽度等于屏幕宽度,dialog无边距,学习总结)