侵入式状态栏适配,网上文章非常多,试了一遍又一遍。。。
最后都能背下几句代码了。。。
1. 先上代码,适配android 6/7/8/9/10,适配刘海屏
public static boolean isNortchScreen = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
this.getNotchParams();
} else {
setStatusBar();
}
}
@TargetApi(28)
public void getNotchParams() {
final View decorView = getWindow().getDecorView();
final MainActivity activity = this;
decorView.post(new Runnable() {
@Override
public void run() {
WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
if (rootWindowInsets == null) {
MainActivity.isNortchScreen = hasNotchInScreen(activity);
activity.setStatusBar();
return;
}
DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
if (displayCutout == null) {
MainActivity.isNortchScreen = hasNotchInScreen(activity);
activity.setStatusBar();
return;
}
// Log.e("TAG", "安全区域距离屏幕左边的距离 SafeInsetLeft:" + displayCutout.getSafeInsetLeft());
// Log.e("TAG", "安全区域距离屏幕右部的距离 SafeInsetRight:" + displayCutout.getSafeInsetRight());
// Log.e("TAG", "安全区域距离屏幕顶部的距离 SafeInsetTop:" + displayCutout.getSafeInsetTop());
// Log.e("TAG", "安全区域距离屏幕底部的距离 SafeInsetBottom:" + displayCutout.getSafeInsetBottom());
List<Rect> rects = displayCutout.getBoundingRects();
if (rects == null || rects.size() == 0) {
MainActivity.isNortchScreen = hasNotchInScreen(activity);
} else {
MainActivity.isNortchScreen = true;
}
activity.setStatusBar();
}
});
}
private void setStatusBar() {
if (Build.VERSION.SDK_INT >= 21) {
if(isNortchScreen) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 齐刘海适配
if (Build.VERSION.SDK_INT >= 28) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
}
if(isNortchScreen) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
2. 对于Android 9.0,网上很多代码:
DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
这一句很多时候直接异常。。。想了一个办法,不在OnCreate里面检查是否刘海屏,起了一个线程去检查,效果很好
3. 华为Nova系列,一开始怎么也适配的很别扭。要把FLAG_FORCE_NOT_FULLSCREEN去掉。但是对于非刘海屏这个属性还不能去掉,所以加了一个判断。
if(isNortchScreen) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
4. hasNotchInScreen是从网上直接copy的,在google/android P判断刘海屏的标准方法不管用的情形下,用这个方法最后挣扎一下,依据实际测试,判断华为手机没有太多作用。
/**
* 是否有刘海屏
*
* @return
*/
public static boolean hasNotchInScreen(Activity activity) {
try {
// 通过其他方式判断是否有刘海屏 目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
String manufacturer = Build.MANUFACTURER;
if (manufacturer.isEmpty()) {
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);
}
}
catch (Exception e) {
}
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;
}
}
5. 以上,收工,觉得有用的话,给我一个赞吧。。。