How to get screen display metrics in application class

前端 未结 6 553
自闭症患者
自闭症患者 2020-11-30 10:38

If I put this in some activity class it works perfectly but, when I put it in my App class the method getWindowManager() can not be found. Is there some way to

6条回答
  •  囚心锁ツ
    2020-11-30 11:27

    final DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    
    getWindow().setLayout((int) (width * .8), (int) (height * .5));
    

提交回复
热议问题