Android获取屏幕宽度有四种方式:
方法一:(过时)
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;