Error getting window size on android: “The method getWindowManager is undefined”

前端 未结 6 2245
半阙折子戏
半阙折子戏 2020-12-18 20:08

I need to get the window height and the weight.
I have this in my code:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDispl         


        
6条回答
  •  無奈伤痛
    2020-12-18 20:50

    Use this:

     display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
     display.getWidth(); // to get width of the screen
     display.getHeight(); // to get height of the Screen
    

    UPDATE

    Please review the Android-Developer site. For more reference.

    You can use below snippet of code to fetch height-width of the device.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

提交回复
热议问题