简体   繁体   English

Android:如何找到屏幕的宽度和高度?

[英]Android: How to find width and height of screen?

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? 我不能使用我在下面尝试的方式,因为.getWidth()已被弃用?

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 = ;

  }
 } 

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM