使用時機不太一樣
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point point= new Point(); display.getSize(point); int screenHeight =point.y; int screenWidth =point.x;
我是寫在Fragment 裡面的,要寫在Activity 那getActivity()要拿掉
Display display = getActivity().getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); int screenHeight = metrics.heightPixels; int screenWidth = metrics.widthPixels; //這樣也可以 final int h = getResources().getDisplayMetrics().heightPixels; final int w = getResources().getDisplayMetrics().widthPixels;