Android 刘海屏全面屏适配

参考链接: 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值