Get screen width and height in Android

前端 未结 30 3637
野的像风
野的像风 2020-11-22 09:28

How can I get the screen width and height and use this value in:

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
    Log.e(TAG, \"onM         


        
相关标签:
30条回答
  • 2020-11-22 10:05

    I use the following code to get the screen dimensions

    getWindow().getDecorView().getWidth()
    getWindow().getDecorView().getHeight()
    
    0 讨论(0)
  • 2020-11-22 10:05
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    

    this may be not work in some case. for the getMetrics comments:

    Gets display metrics that describe the size and density of this display. The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display.

    1. The returned size may be adjusted to exclude certain system decor elements that are always visible.

    2. It may be scaled to provide compatibility with older applications that were originally designed for smaller displays.

    3. It can be different depending on the WindowManager to which the display belongs.

    • If requested from non-Activity context (e.g. Application context via (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)) metrics will report the size of the entire display based on current rotation and with subtracted system decoration areas.

    • If requested from activity (either using getWindowManager() or (WindowManager) getSystemService(Context.WINDOW_SERVICE)) resulting metrics will correspond to current app window metrics. In this case the size can be smaller than physical size in multi-window mode.

    So, to get the real size:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
    

    or:

    Point point = new Point();
    getWindowManager().getDefaultDisplay().getRealSize(point);
    int height = point.y;
    int width = point.x;
    
    0 讨论(0)
  • 2020-11-22 10:06

    Try below code :-

    1.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

    2.

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated
    

    or

    int width = getWindowManager().getDefaultDisplay().getWidth(); 
    int height = getWindowManager().getDefaultDisplay().getHeight();
    

    3.

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    metrics.heightPixels;
    metrics.widthPixels;
    
    0 讨论(0)
  • 2020-11-22 10:06

    Get the value of screen width and height.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;
    
    0 讨论(0)
  • 2020-11-22 10:08

    I found weigan's answer best one in this page, here is how you can use that in Xamarin.Android:

    public int GetScreenWidth()
    {
        return Resources.System.DisplayMetrics.WidthPixels;
    }
    
    public int GetScreenHeight()
    {
        return Resources.System.DisplayMetrics.HeightPixels;
    }
    
    0 讨论(0)
  • 2020-11-22 10:09
    Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int mWidthScreen = display.getWidth();
    int mHeightScreen = display.getHeight();
    
    0 讨论(0)
提交回复
热议问题