方法一:
WindowManager wm = (WindowManager) this .getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();
方法二:
WindowManager wm1 = this.getWindowManager(); int width1 = wm1.getDefaultDisplay().getWidth(); int height1 = wm1.getDefaultDisplay().getHeight();
方法一与方法二获取屏幕宽度的方法类似,只是获取WindowManager 对象时的途径不同。
方法三:
WindowManager manager = this.getWindowManager(); DisplayMetrics outMetrics = new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; int height = outMetrics.heightPixels;
方法四:
Resources resources = this.getResources(); DisplayMetrics dm = resources.getDisplayMetrics(); float density = dm.density; int width = dm.widthPixels; int height = dm.heightPixels;
方法三与方法四类似。
因 Display.getHeight(),Display.getWidth() 方法过时推荐使用方法三与四