检查设备是否有缺口

时间:2018-12-02 09:58:45

标签: android android-hardware windowinsets

我有一个带有叠加层的应用程序,我需要检查设备是否有缺口。

通常情况下,您使用该活动并拨打以下电话:

activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout()

是否有其他方法可以检查服务中是否存在缺口?如果设备有缺口,我需要调整覆盖层。

如果我只能访问WindowManager,是否可以找到设备是否有缺口?

1 个答案:

答案 0 :(得分:0)

当前,要检测设备是否具有带有Android API的陷波屏(Android

    int statusBarHeight = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }


  // DP to Pixels

    public static int convertDpToPixel ( float dp){
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return Math.round(px);
    }

   // Make UI adjustments as per your requirement

    if (statusBarHeight > convertDpToPixel(24)) {
        RelativeLayout.LayoutParams topbarLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        topbarlp.setMargins(0, statusBarHeight, 0, 0);
        topbar.setLayoutParams(topbarlp)
    }
相关问题