android之获取屏幕的宽度和高度

jopen 10年前

获取屏幕的宽度和高度:

方法一:

           //获取屏幕的宽度        public static int getScreenWidth(Context context) {            WindowManager manager = (WindowManager) context                    .getSystemService(Context.WINDOW_SERVICE);            Display display = manager.getDefaultDisplay();            return display.getWidth();        }        //获取屏幕的高度        public static int getScreenHeight(Context context) {            WindowManager manager = (WindowManager) context                    .getSystemService(Context.WINDOW_SERVICE);            Display display = manager.getDefaultDisplay();            return display.getHeight();        }  
</div> </div>



 方法二:

DisplayMetrics  dm = new DisplayMetrics();            getWindowManager().getDefaultDisplay().getMetrics(dm);            int screenWidth = dm.widthPixels;                      int screenHeight = dm.heightPixels;    
</div> </div>