在内部类中使用:
<pre>
//获取屏幕的宽度旧方法,已废弃
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
System.out.println("screenWidth:" + screenWidth);
//获取屏幕的宽高新方法1
Point size = new Point();
wm.getDefaultDisplay().getSize(size);
int screenWidth1 = size.x;
int screenHeight = size.y;
System.out.println("screenWidth1:" + screenWidth1);
//获取屏幕的宽高新方法2
DisplayMetrics displaymetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth2 = displaymetrics.widthPixels;
int screenHeight2 = displaymetrics.heightPixels;
System.out.println("screenWidth2:" + screenWidth2);
</pre>
在activity里使用:
<pre>
//方法1
DisplayMetrics dm = new
DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
//方法2
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
</pre>
网友评论