如何检测设备是否支持缺口显示? [英] How to detect if device support notch display?

查看:59
本文介绍了如何检测设备是否支持缺口显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在检测移动设备是否支持android中的缺口显示时遇到问题.

Currently I am facing a problem to detect if mobile device support notch display in android.

有人可以帮助我吗? (我需要使用android studio中的代码来完成此操作)

Can any one help me in this? (I need to do this using code in android studio)

谢谢.

推荐答案

如果您打算支持所有操作系统,则某些Oreo设备也会显示缺口,那么您可以使用我的解决方案.根据材料设计指南,Android设备的状态栏高度为24dp.您可以使用以下方法获取设备状态栏的高度和设备密度,并检查状态栏的高度是否大于24dp.如果其高度大于24dp,则显示为缺口,然后您可以根据需要处理视图位置.这也适用于奥利奥(Oreo).

Some Oreo devices also have notch display if you are targeting to support all OS then you can use my solution. As per material design guidelines the status bar height for Android devices is 24dp. You can get device status bar height and device density by using the following and check if status bar height is more than 24dp. If its height is more than 24dp then it has the notch on display and then you can handle your view position as per your requirement. This will work on Oreo as well.

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

//DP转换为像素

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

//根据您的要求进行UI调整

// 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);

    //Set above layout params to your layout which was getting cut because of notch
    topbar.setLayoutParams(topbarlp)
}

这篇关于如何检测设备是否支持缺口显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆