Android刘海屏、水滴屏全面屏适配方案,2021年最新大厂Android面试笔试题目

// 延伸显示区域到刘海
WindowManager.LayoutParams lp = window.getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(lp);
// 设置页面全屏显示
final View decorView = window.getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

其中延伸显示区域到刘海的代码,也可以通过修改Activity或应用的style实现,例如:

<?xml version="1.0" encoding="utf-8"?>

Android O 适配

因Google官方的适配方案到Android P才推出,因此在Android O设备上,各家厂商有自己的实现方案。

我这里主要适配了华为、小米、oppo,这三家都给了完整的解决方案。至于vivo,vivo给了判断是否刘海屏的API,但是没用设置刘海区域显示到API,因此无需适配。

适配华为Android O设备

方案一:

  1. 具体方式如下所示:
  1. 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理:



<activity androi

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取

d:name=".MainActivity">




  1. 对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理:




方案二

对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理

我的NotchScreenTool中使用的就是方案二,如果需要针对Activity,建议自行修改。

  1. 设置应用窗口在华为刘海屏手机使用刘海区

/刘海屏全屏显示FLAG/
public static final int FLAG_NOTCH_SUPPORT=0x00010000;
/**

  • 设置应用窗口在华为刘海屏手机使用刘海区
  • @param window 应用页面window对象
    */
    public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {
    if (window == null) {
    return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
    Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
    Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
    Object layoutParamsExObj=con.newInstance(layoutParams);
    Method method=layoutParamsExCls.getMethod(“addHwFlags”, int.class);
    method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
    | InvocationTargetException e) {
    Log.e(“test”, “hw add notch screen flag api error”);
    } catch (Exception e) {
    Log.e(“test”, “other Exception”);
    }
    }
  1. 清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示

/**

  • 设置应用窗口在华为刘海屏手机使用刘海区
  • @param window 应用页面window对象
    */
    public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {
    if (window == null) {
    return;
    }
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    try {
    Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
    Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
    Object layoutParamsExObj=con.newInstance(layoutParams);
    Method method=layoutParamsExCls.getMethod(“clearHwFlags”, int.class);
    method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
    | InvocationTargetException e) {
    Log.e(“test”, “hw clear notch screen flag api error”);
    } catch (Exception e) {
    Log.e(“test”, “other Exception”);
    }
    }
适配小米Android O设备
  1. 判断是否是刘海屏

private static boolean isNotch() {
try {
Method getInt = Class.forName(“android.os.SystemProperties”).getMethod(“getInt”, String.class, int.class);
int notch = (int) getInt.invoke(null, “ro.miui.notch”, 0);
return notch == 1;
} catch (Throwable ignore) {
}
return false;
}

  1. 设置显示到刘海区域

@Override
public void setDisplayInNotch(Activity activity) {
int flag = 0x00000100 | 0x00000200 | 0x00000400;
try {
Method method = Window.class.getMethod(“addExtraFlags”,
int.class);
method.invoke(activity.getWindow(), flag);
} catch (Exception ignore) {
}
}

  1. 获取刘海宽高

public static int getNotchHeight(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_height”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}

public static int getNotchWidth(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_width”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}

适配oppoAndroid O设备
  1. 判断是否是刘海屏

@Override
public boolean hasNotch(Activity activity) {
boolean ret = false;
try {
ret = activity.getPackageManager().hasSystemFeature(“com.oppo.feature.screen.heteromorphism”);
} catch (Throwable ignore) {
}
return ret;
}

  1. 获取刘海的左上角和右下角的坐标

/**

  • 获取刘海的坐标
  • 属性形如:[ro.oppo.screen.heteromorphism]: [378,0:702,80]
  • 获取到的值为378,0:702,80
  • (378,0)是刘海区域左上角的坐标
  • (702,80)是刘海区域右下角的坐标
    */
    private static String getScreenValue() {
    String value = “”;
    Class<?> cls;
    try {
    cls = Class.forName(“android.os.SystemProperties”);
    Method get = cls.getMethod(“get”, String.class);
    Object object = cls.newInstance();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值