Get screen width and height in Android

前端 未结 30 4171
野的像风
野的像风 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:12

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
      public static double getHeight() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getRealMetrics(displayMetrics);
    
        //int height = displayMetrics.heightPixels;
        //int width = displayMetrics.widthPixels;
        return displayMetrics.heightPixels;
      }
    

    Using that method you can get screen height. if you want to get width change displayMetrics.heightPixels to displayMetrics.widthPixels.

    And it also include required api Build version.

提交回复
热议问题