210_Dialog和AlertDialog各种情况调试
Dialog和AlertDialog有很多的不同
然后不同的情况下又会有很多的不同
比如先show还是先setContentView
有时候出来的情况就不一样
我就把调试的结果全部总结了然后比较一下
先是Dialog的先填充还是直接给R.layout,还有getWindow再设置setContentView和直接设置
public void open1(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
Dialog dialog = new Dialog(context);
dialog.show();
dialog.getWindow().setContentView(inflate);
}
public void open2(View view) {
Dialog dialog = new Dialog(context);
dialog.show();
dialog.setContentView(R.layout.dialog);
}
public void open3(View view) {
Dialog dialog = new Dialog(context);
dialog.show();
dialog.getWindow().setContentView(R.layout.dialog);
}
public void open4(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
Dialog dialog = new Dialog(context);
dialog.show();
dialog.setContentView(inflate);
}
这里的4种情况是一样的,也就是说是否先填充是一样的
getWindow这里虽然是一样的,但是AlertDialog就不一样了
这里要搞清楚对dialog设置setContentView和对window设置setContentView的区别
下面是AlertDialog的setContentView的不同情况
public void open3(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.setContentView(R.layout.dialog);
}
public void open4(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.getWindow().setContentView(R.layout.dialog);
}
两个显示很像,但是第二个比第一个窗口要小一点
所以对AlertDialog设置和对window设置是有区别的
然后我们看看想设置AlertDialog占满屏幕的不同情况
我们的布局很简单,width给的是match,height给的是wrap
public void open1(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.setContentView(R.layout.dialog);
WindowManager.LayoutParams attributes = alertDialog.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
alertDialog.getWindow().setAttributes(attributes);
}
public void open2(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.setContentView(inflate);
WindowManager.LayoutParams attributes = alertDialog.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
alertDialog.getWindow().setAttributes(attributes);
}
public void open3(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.getWindow().setContentView(R.layout.dialog);
WindowManager.LayoutParams attributes = alertDialog.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
alertDialog.getWindow().setAttributes(attributes);
}
public void open4(View view) {
View inflate = View.inflate(context, R.layout.dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.show();
alertDialog.getWindow().setContentView(inflate);
WindowManager.LayoutParams attributes = alertDialog.getWindow().getAttributes();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
alertDialog.getWindow().setAttributes(attributes);
}
我们发现,本来给window设置setContentView,会比直接对dialog设置小一点,也就是窄一点
但是现在4个都一样宽了,也就是说match这个属性对window起作用了
另外一点就是这说明了AlertDialog最宽就是这么宽,和屏幕就是会有一段距离
所以如果想充满屏幕得想其他办法,光给match是不够的.
然后我们试着不同的情况去占满屏幕
public void open2(View view) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
dialog.setContentView(R.layout.dialog);
Window window = dialog.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);
}
public void open3(View view) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
}
public void open4(View view) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.getDecorView().setPadding(0, 0, 0, 0);
}
我们发现第一个成功了,
第二个上下左右都还是和屏幕有一段距离
第三个左右的间距还存在,上下没有间距了.