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

Tom*_*652 4 height android pixel android-statusbar

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

在花了一周的时间研究这个问题并检查了 Google 的文档以及 Stackoverflow 上的许多其他帖子(关于通过getSize()getRealSize()getMetrics(DisplayMetrics metrics)getRealMetrics(DisplayMetrics metrics)等获取屏幕尺寸(以像素为单位)......我仍然对此一无所知。

这张图更好地描述了这个问题。

在某些手机上,根据上图,线“Y/2”应该正好位于size.y / 2(导航栏和状态栏之间屏幕的一半),但实际上不在中间:“Not X”距离因此,我的图片上的 等于“X - getStatusBarSize()”。

我必须补充一点,我正在使用 XML 中的 innerRadiusRatio 2.1 的环形对象。但我不认为这是原因,因为一旦我使用下面的代码,它就适用于 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;
    }

Run Code Online (Sandbox Code Playgroud)

虽然它可以在其他手机上简单地使用maxheight = size.y(不包括状态栏高度)。

预先感谢大家的帮助!

nic*_*er 5

我或多或少面临着同样的问题,这可能会有所帮助:

// 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();
}
Run Code Online (Sandbox Code Playgroud)