代码之家  ›  专栏  ›  技术社区  ›  prom85

检查设备是否有缺口

  •  1
  • prom85  · 技术社区  · 7 年前

    我有一个带有覆盖的应用程序,我需要检查设备是否有凹口。

    通常使用活动并调用以下命令:

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

    有没有其他方法可以检查服务中也可以使用的槽口?我需要调整我的覆盖,如果设备有缺口…

    如果我只能访问 WindowManager ?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sachin Jagtap    7 年前

    目前,检测设备是否有带有android api(android<p)的notch屏幕是不可行的。所以我找到了一份工作。Android设备的状态栏高度为24dp。您可以使用以下命令获取设备状态栏高度和设备密度,并检查状态栏高度是否大于24 dp。如果它的高度超过24 dp,那么它在显示器上有凹口,然后你可以根据你的要求处理你的视图位置。这对我所有版本都有效。

        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)
        }