getHeight()和getWidth() deprecated in API level 13

Display dp=getWindowManager().getDefaultDisplay();
int height=dp.getHeight();
int width=dp.getWidth();
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

官方文档

获取屏幕的宽和高-Display中getHeight()和getWidth() 官方已废弃-LMLPHP

获取屏幕的宽和高-Display中getHeight()和getWidth() 官方已废弃-LMLPHP

instead method

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

或者

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
05-11 11:32