我正在尝试查找屏幕的宽度和高度,但我尝试过的任何方法都不适用于我创建的类,我的代码如下。
有谁知道如何找到它?我无法使用我在下面尝试的方式,因为 .getWidth() 已被弃用?
public class Crate {
public int acrossCrate;
public int upDownCrate;
Context theContext;
WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
public Crate() {
acrossCrate = 650;
upDownCrate = 650;
//int width = ;
//int height = ;
}
}
使用以下代码
private int getScreenHeight(Context context) {
int height;
if (android.os.Build.VERSION.SDK_INT >= 13) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
} else {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
height = display.getHeight(); // deprecated
}
return height;
}