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

2021-08-16 10:23:46 浏览数 (2)

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

代码语言:javascript复制
Display dp=getWindowManager().getDefaultDisplay();
int height=dp.getHeight(); 
int width=dp.getWidth(); 

官方文档

instead method

代码语言:javascript复制
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

或者

代码语言:javascript复制
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width  = metrics.widthPixels;

0 人点赞