获取安卓刘海高度

import android.os.Build;
static int screenTopMargin = 0;
static int screenBottomMargin = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {

.......

if (Build.VERSION.SDK_INT >= 28) {
    getWindow().getDecorView().setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
            Log.e("notch", "onApplyWindowInsets");
            if(windowInsets == null){
                return windowInsets;
            }
            DisplayCutout cutout = windowInsets.getDisplayCutout();
            if (cutout == null) {
                Log.e("notch", "cutout==null, is not notch screen");//通过cutout是否为null判断是否刘海屏手机

            } else {
                List<Rect> rects = cutout.getBoundingRects();
                if (rects == null || rects.size() == 0) {
                    Log.e("notch", "rects==null || rects.size()==0, is not notch screen");
                } else {
                    Log.e("notch", "rect size:" + rects.size());//注意:刘海的数量可以是多个
                    for (Rect rect : rects) {
                        Log.e("notch", "cutout.getSafeInsetTop():" + cutout.getSafeInsetTop()
                                + ", cutout.getSafeInsetBottom():" + cutout.getSafeInsetBottom()
                                + ", cutout.getSafeInsetLeft():" + cutout.getSafeInsetLeft()
                                + ", cutout.getSafeInsetRight():" + cutout.getSafeInsetRight()
                                + ", cutout.rects:" + rect
                        );
                    }

                    screenTopMargin = cutout.getSafeInsetTop();
                    screenBottomMargin = cutout.getSafeInsetBottom();
                }
            }
            return windowInsets;
        }
    });
}else {
    screenTopMargin = this.getStatusBarHeight(this.getResources());
    screenBottomMargin = 0;
}

}

 

public static int getStatusBarHeight(Resources resources) {
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

 

public static int getScreenTopMargin() {
    return screenTopMargin;
}

public static int getScreenBottomMargin() {
    return screenBottomMargin;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值