目前,检测设备是否有带有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)
}