Android: How to find width and height of screen?

霸气de小男生 提交于 2019-12-19 09:12:32

问题


I am trying to find the width and height of the screen but none of the ways I have tried will work in the class I created, my code is below.

Does anyone know how to find it? I cannot use the way I am trying below because .getWidth() is deprecated?

public class Crate {

    public int acrossCrate;
    public int upDownCrate;
    Context theContext;

    WindowManager wm = (WindowManager)theContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = display.getWidth();


    public Crate() {
    acrossCrate = 650;
    upDownCrate = 650;
    //int width = ;
    //int height = ;

  }
 } 

回答1:


Use below code

private int getScreenHeight(Context context) {
        int height;

        if (android.os.Build.VERSION.SDK_INT >= 13) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            height = size.y;
        } else {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            height = display.getHeight();  // deprecated
        }

        return height;
    }


来源:https://stackoverflow.com/questions/30929519/android-how-to-find-width-and-height-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!