Android: How to get the real screen size of the device?

前端 未结 4 1114
温柔的废话
温柔的废话 2021-02-05 18:11

I have tried different ways to get the screen size of the device, but it always returned me the wrong size (791x480 instead of 854x480). It might be du

4条回答
  •  盖世英雄少女心
    2021-02-05 18:48

    if you want to get the size in the activity, you can make it by the following code.

    findViewById(android.R.id.content).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                View view =  findViewById(android.R.id.content);
                Log.e("test", "root view height is " + view.getHeight());
    
            }
        });
    

提交回复
热议问题