默认的Dialog是不能全屏的。也就是怎么设置Dialog的Layout和通过
Window window = mDialog.getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = (int) (screenUtils.getWidth());
layoutParams.height = (int) (screenUtils.getHeight());
都没用的。
给出实现Dialog实现全屏的两种方式:
1、代码实现。这中方法相对比较简单
首先继承Dialig,然后再构造函数中添加
super(context, android.R.style.Theme);
setOwnerActivity((Activity)context);
这里面显示Dialog是将状态栏也隐藏掉了,如果不想隐藏就指定长宽,可以采用设置layoutParams.长宽属性,然后通过
WindowManager.LayoutParams layoutParams = window.getAttributes();
这里面的layoutParams 去作为新的参数传给windows
即:window.setAttributes(layoutParams);
2、XML实现
首先,在values文件中添加一个XML文件,
其次,在XML文件中设置一个style
dialog.setContentView(R.layout.main);
dialog.show();
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
/**
* 获取屏幕的分辨率帮助类
* @param context
* @return width or height
* @see ScreenUtils
* */
public class ScreenUtils {
private int width, height;
private Context context;
public ScreenUtils(Context context) {
this.context = context;
init();
}
private void init() {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
width = outMetrics.widthPixels;
height = outMetrics.heightPixels;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
【欢迎上码】
【微信公众号搜索 h2o2s2】