Loading

刘海屏适配踩坑

今天在EMUI上获取状态栏高度结果只有6像素,这算是EMUI的bug吧。所以加了个判断来获取状态栏高度。让状态栏至少为25dp

    public static int getStatusBarHeight(@NonNull Context context) {
        int height = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = context.getResources().getDimensionPixelSize(resourceId);
        }
        if (height < dp2px(context, 10)) {
            height = dp2px(context, 25);
        }
        return height;
    }


    /**
     * convert dp to its equivalent px
     * <p>
     * 将dp转换为与之相等的px
     */
    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
posted @ 2022-03-26 12:45  徐影魔  阅读(37)  评论(0)    收藏  举报