参考链接: https://blog.csdn.net/u011494285/article/details/86681405

API >= 24 (Android 7.0) 以上会自动适配全面屏, 但项目中发现嵌套在 Activity 中的 Fragment 在华为的刘海机型上有异常显示. 所以花了些时间适配.

工具类:

NotchUtils.class

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.view.DisplayCutout;

import java.lang.reflect.Method;

/**
 * Created by geekqian on 2019/11/18.
 * 描述: 刘海屏判断工具类
 */
public class NotchUtils {
    /**
     * 是否有刘海屏
     *
     * @return
     */
    public static boolean hasNotchInScreen(Activity activity) {
        try {
            // android  P 以上有标准 API 来判断是否有刘海屏
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
                if (displayCutout != null) {
                    // 说明有刘海屏
                    return true;
                }
            } else {
                // 通过其他方式判断是否有刘海屏  目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
                String manufacturer = Build.MANUFACTURER;
                if (TextUtils.isEmpty(manufacturer)) {
                    return false;
                } else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
                    return hasNotchHw(activity);
                } else if (manufacturer.equalsIgnoreCase("xiaomi")) {
                    return hasNotchXiaoMi(activity);
                } else if (manufacturer.equalsIgnoreCase("oppo")) {
                    return hasNotchOPPO(activity);
                } else if (manufacturer.equalsIgnoreCase("vivo")) {
                    return hasNotchVIVO(activity);
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            LogUtils.print(e.getMessage());
            return false;
        }
        return false;
    }

    /**
     * 判断vivo是否有刘海屏
     * https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchVIVO(Activity activity) {
        try {
            Class<?> c = Class.forName("android.util.FtFeature");
            Method get = c.getMethod("isFeatureSupport", int.class);
            return (boolean) (get.invoke(c, 0x20));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断oppo是否有刘海屏
     * https://open.oppomobile.com/wiki/doc#id=10159
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchOPPO(Activity activity) {
        return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    /**
     * 判断xiaomi是否有刘海屏
     * https://dev.mi.com/console/doc/detail?pId=1293
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchXiaoMi(Activity activity) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("getInt", String.class, int.class);
            return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断华为是否有刘海屏
     * https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
     *
     * @param activity
     * @return
     */
    private static boolean hasNotchHw(Activity activity) {

        try {
            ClassLoader cl = activity.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            return (boolean) get.invoke(HwNotchSizeUtil);
        } catch (Exception e) {
            return false;
        }
    }

}

产生异常是因为刘海屏自动适配时把 Layout 下移了一个状态栏的高度, 所以处理方式是代码动态的把底部的控件向上偏移状态栏的高度, 在 FragmentonViewCreated 方法中, 使用:

if (NotchUtils.hasNotchInScreen(mActivity)){
        ViewGroup.LayoutParams params = mRlInput.getLayoutParams();
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(params);
        int barHeight = StatusBarUtil.getStatusBarHeight(mActivity);
        layoutParams.setMargins(0,0,0, barHeight);//4个参数按顺序分别是左上右下
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mRlInput.setLayoutParams(layoutParams);
}

getStatusBarHeight(activity)

/**
 * 获取状态栏的高度
 */
public static int getStatusBarHeight(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return 0;
    }
    Resources resources = activity.getResources();
    int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
    return resources.getDimensionPixelOffset(identifier);
}