Android中的getSize()是否包含状态栏的高度?

Android中的getSize()是否包含状态栏的高度?,android,height,pixel,android-statusbar,Android,Height,Pixel,Android Statusbar,有没有办法判断getSize()是否包含状态栏的高度?从在几个不同的手机上进行的测试来看,它似乎在一些手机上可以做到,但在其他手机上却不能做到。当试图使布局中的所有内容正确排列时,这是令人沮丧的 在花了一周的时间研究这个问题并查看了Google的文档和Stackoverflow上的许多其他帖子(关于通过getSize(),getRealSize(),getMetrics(DisplayMetrics),getRealMetrics(DisplayMetrics)获取屏幕的像素大小),等等……我还

有没有办法判断
getSize()
是否包含状态栏的高度?从在几个不同的手机上进行的测试来看,它似乎在一些手机上可以做到,但在其他手机上却不能做到。当试图使布局中的所有内容正确排列时,这是令人沮丧的

在花了一周的时间研究这个问题并查看了Google的文档和Stackoverflow上的许多其他帖子(关于通过
getSize()
getRealSize()
getMetrics(DisplayMetrics)
getRealMetrics(DisplayMetrics)获取屏幕的像素大小)
,等等……我还是不知道

在某些电话上,根据上面的图片,“y/2”线应该完全在<代码>大小。y/2</代码>(导航和状态栏之间的屏幕的确切一半),但实际上在中间是“强”>“不”/“强”:因此,在我的图片上的“不x”距离等于'x-GestStubSusiZe()。

我必须补充一点,我使用的是XML格式的innerRadiusRatio为2.1的ring对象。 但我不认为这是原因,因为一旦我使用了下面的代码,它就可以在getSize()似乎包含状态栏高度的手机上工作:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Screen dimensions
        display = getWindowManager().getDefaultDisplay();
        size = new Point();
        display.getSize(size);

       //Get the screen dimensions
       maxWidth = size.x 
       maxHeight = size.y - getStatusBarSize();
   }

private int getStatusBarSize() {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }

而在其他手机上(不包括状态栏高度),它只适用于
maxheight=size.y


提前感谢各位的帮助!

我或多或少也遇到了同样的问题,这可能会有所帮助:

// Get the size between the status & the navigation bars
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

// Real size of the screen (full screen)
Point realSize = new Point();
Display realDisplay = getWindowManager().getDefaultDisplay();
realDisplay.getRealSize(realSize);

//Get the screen dimensions
maxWidth = size.x;
maxHeight = size.y;

if (realSize.y - getNavBarSize() == size.y || realSize.y == size.y) {
    Log.d(TAG, "onCreate: getSize() includes the status bar size");
    maxHeight = size.y - getStatusBarSize();
}

我也有同样的问题,我就是这样解决的

  • Display.getSize()有时仅包括状态栏高度
  • 对于使用.getRealSize()的解决方案,还需要导航栏的高度
使用以下解决方案,您不需要导航栏高度:

View contentView = requireActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT);
int height = contentView.getHeight() - statusBarHeight - actionBarHeight;

(如果返回0,您可能必须在contentView.post()中获取高度。)

我可以知道您想要实现什么吗?也许有另一种方法可以更有效地实现这一点。我想要实现的是用数学(动态)将屏幕高度减半在一些手机上,我可以看到屏幕的一半+
statusBarHeight
。我可以注意到这一点,因为在我的主布局中有一个固定的符号,在任何其他情况下我都不会注意到。这正是我想要的,谢谢。所以答案是肯定的,有时候
getSize()
方法确实包括状态栏高度。。。