// 延伸显示区域到刘海
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设备
方案一:
- 具体方式如下所示:
- 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理:
<activity androi
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
d:name=".MainActivity">
- 对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理:
方案二
对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理
我的NotchScreenTool中使用的就是方案二,如果需要针对Activity,建议自行修改。
- 设置应用窗口在华为刘海屏手机使用刘海区
/刘海屏全屏显示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”);
}
}
- 清除添加的华为刘海屏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设备
- 判断是否是刘海屏
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;
}
- 设置显示到刘海区域
@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) {
}
}
- 获取刘海宽高
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设备
- 判断是否是刘海屏
@Override
public boolean hasNotch(Activity activity) {
boolean ret = false;
try {
ret = activity.getPackageManager().hasSystemFeature(“com.oppo.feature.screen.heteromorphism”);
} catch (Throwable ignore) {
}
return ret;
}
- 获取刘海的左上角和右下角的坐标
/**
- 获取刘海的坐标
-
- 属性形如:[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();