Display中getHeight()和getWidth() 官方废弃

xiaoxiao2021-02-28  86

今天使用Display获取屏幕的宽和高时出现下面的提示(而且在部分5.0机型得到的height都是错误):

Display dp=getWindowManager().getDefaultDisplay(); int Height=dp.getHeight(); ---->The method getHeight() from the type Display is deprecated int Width=dp.getWidth(); ---->The method getWidth() from the type Display is deprecated 官方解释: int    getHeight() This method was deprecated in API level 13. Use getSize(Point) instead.  int     getWidth() This method was deprecated in API level 13. Use getSize(Point) instead.  http://developer.android.com/reference/android/view/Display.html 替代的方法: DisplayMetrics dm = new DisplayMetrics();                 getWindowManager().getDefaultDisplay().getMetrics(dm); int screen_wight = dm.widthPixels; int screen_height = dm.heightPixels; 解析heightPixels和widthPixels: public int     heightPixels     The absolute height of the display in pixels. public int     widthPixels     The absolute width of the display in pixels.

转载请注明原文地址: https://www.6miu.com/read-64167.html

最新回复(0)