Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取状态栏高度 #569

Open
604779042 opened this issue Nov 2, 2023 · 0 comments
Open

获取状态栏高度 #569

604779042 opened this issue Nov 2, 2023 · 0 comments

Comments

@604779042
Copy link

 int resourceId = Resources.getSystem().getIdentifier(key, "dimen", "android");
        if (resourceId > 0) {
            int sizeOne = context.getResources().getDimensionPixelSize(resourceId);
            int sizeTwo = Resources.getSystem().getDimensionPixelSize(resourceId);
            // android9以上版本,可以通过修改开发者选项里的刘海屏,Resources.getSystem()也会导致获得的状态栏高度不准确,
            // 可以通过context.getResources()方式来获取准确的高度。
            // 假如android9以上手机又使用了今日头条适配方案而且开发者选项里开启了刘海屏,那样两种方式获得density有可能不一样,
            // 所以通过densityTwo / densityOne来修复正确的高度
            if (sizeTwo >= sizeOne &&
                    !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !key.equals("status_bar_height"))) {
                result = sizeTwo;
            } else {
                float densityOne = context.getResources().getDisplayMetrics().density;
                float densityTwo = Resources.getSystem().getDisplayMetrics().density;
                float f = sizeOne * densityTwo / densityOne;
                result = (int) ((f >= 0) ? (f + 0.5f) : (f - 0.5f));
            }
        }

想问下获取状态栏高度的时候,为啥,sizeTwo>=sizeOne的时候取这个sizeTwo。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant