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

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

    Use the Context of the activity with getActivityContext()

    DisplayMetrics metrics = new DisplayMetrics();
    getActivityContext().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    width1 = metrics.widthPixels; 
    height1 = metrics.heightPixels;
    

提交回复
热议问题