getSize() giving me errors

前端 未结 7 2140
谎友^
谎友^ 2020-11-27 18:30

When I implement

WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
m_nDisplayWi         


        
相关标签:
7条回答
  • 2020-11-27 19:02

    Try this, it works for me.

    getWindowManager().getDefaultDisplay().getWidth();
    
    0 讨论(0)
  • 2020-11-27 19:03

    Here is the utility method, you could utilize:

    public static int getWidth(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        int width;
        if (Build.VERSION.SDK_INT >= 13) {
            display.getSize(size);
            width = size.x;
        } else {
            width = display.getWidth();
        }
        return width;
    }
    
    0 讨论(0)
  • 2020-11-27 19:10

    This supports both older and newer devices:

    private static Point getDisplaySize(final Display display) {
        final Point point = new Point();
        try {
            display.getSize(point);
        } catch (java.lang.NoSuchMethodError ignore) { // Older device
            point.x = display.getWidth();
            point.y = display.getHeight();
        }
        return point;
    }
    
    0 讨论(0)
  • 2020-11-27 19:12

    Sorry that I'm late to the party, but make sure you're using android sdk 13 or above.

    0 讨论(0)
  • 2020-11-27 19:19

    This version works for API < 4, too:

    @SuppressWarnings("deprecation")
    @TargetApi(VERSION_CODES.HONEYCOMB_MR2)
    public static Point getSize(Display display) {
        Point point = new Point();
        if (sdkInt() >= VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(point);
        } else {
            point.set(display.getWidth(), display.getHeight());
        }
        return point;
    }
    
    @SuppressWarnings("deprecation")
    public static int sdkInt() {
        return Integer.parseInt(VERSION.SDK);
    }
    
    0 讨论(0)
  • 2020-11-27 19:25

    Try to use that instead :

    Display display = getWindowManager().getDefaultDisplay();
    

    Without that, i don't see any problem in this code ...

    0 讨论(0)
提交回复
热议问题