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

前端 未结 6 2235
半阙折子戏
半阙折子戏 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:47

    Where are you using this code?

    The error of the getWindowManager says that cannot find the method because the root of your class doesn't have it.

    Try this:

    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    

    or

    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
      .getDefaultDisplay().getMetrics(metrics);
    

    If this doesn't work, please paste your activity

提交回复
热议问题