- res下新建values-28文件夾,新建values-style下同名主題
- activity 初始化中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
View decorView = window.getDecorView();
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0,
defaultInsets.getSystemWindowInsetRight(),
defaultInsets.getSystemWindowInsetBottom());
}
});
ViewCompat.requestApplyInsets(decorView);
//將狀態欄設成透明,如不想透明可設置其餘顏色
window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
複製代碼
- 從新計算屏幕高度
//5.0往上採用全屏顯示
if (Build.VERSION.SDK_INT > 19) {
if (NotchUtil.hasNotchInScreen(ActivityStack.getInstance().topActivity())) {
mHeight = NotchUtil.getFullActivityHeight(App.getApp());
}else {
mHeight = context.getResources().getDisplayMetrics().heightPixels;
}
} else {
//4.4及如下采用去除標題欄
mHeight = context.getResources().getDisplayMetrics().heightPixels - BarUtils.getStatusBarHeight();
}
複製代碼
工具
public class NotchUtil {
/**
* 小米劉海屏判斷.
*/
public static int getInt(String key, Activity activity) {
int result = 0;
if (isXiaomi()) {
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
//參數類型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//參數
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
// 是不是小米手機
public static boolean isXiaomi() {
return "Xiaomi".equals(Build.MANUFACTURER);
}
/**
* 華爲劉海屏判斷
*/
public static boolean hasNotchAtHuawei(Context context) {
boolean ret = false;
try {
ClassLoader cl = context.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
ret = (boolean) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
/**
* VIVO劉海屏判斷
*/
public static final int VIVO_NOTCH = 0x00000020;//是否有劉海
public static final int VIVO_FILLET = 0x00000008;//是否有圓角
public static boolean hasNotchAtVivo(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class FtFeature = classLoader.loadClass("android.util.FtFeature");
Method method = FtFeature.getMethod("isFeatureSupport", int.class);
ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
/**
* OPPO劉海屏判斷
*/
public static boolean hasNotchAtOPPO(Context context) {
return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
public static int getFullActivityHeight(@Nullable Context context) {
if (!isAllScreenDevice()) {
return getScreenHeight(context);
}
return getScreenRealHeight(context);
}
private static final int PORTRAIT = 0;
private static final int LANDSCAPE = 1;
@NonNull
private volatile static Point[] mRealSizes = new Point[2];
public static int getScreenRealHeight(@Nullable Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return getScreenHeight(context);
}
int orientation = context != null
? context.getResources().getConfiguration().orientation
: App.getApp().getResources().getConfiguration().orientation;
orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;
if (mRealSizes[orientation] == null) {
WindowManager windowManager = context != null
? (WindowManager) context.getSystemService(Context.WINDOW_SERVICE)
: (WindowManager) App.getApp().getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return getScreenHeight(context);
}
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
mRealSizes[orientation] = point;
}
return mRealSizes[orientation].y;
}
public static int getScreenHeight(@Nullable Context context) {
if (context != null) {
return context.getResources().getDisplayMetrics().heightPixels;
}
return 0;
}
private volatile static boolean mHasCheckAllScreen;
private volatile static boolean mIsAllScreenDevice;
public static boolean isAllScreenDevice() {
if (mHasCheckAllScreen) {
return mIsAllScreenDevice;
}
mHasCheckAllScreen = true;
mIsAllScreenDevice = false;
// 低於 API 21的,都不會是全面屏。。。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
WindowManager windowManager = (WindowManager) App.getApp().getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
float width, height;
if (point.x < point.y) {
width = point.x;
height = point.y;
} else {
width = point.y;
height = point.x;
}
if (height / width >= 1.97f) {
mIsAllScreenDevice = true;
}
}
return mIsAllScreenDevice;
}
/**
* 是否有劉海屏
*
* @return
*/
public static boolean hasNotchInScreen(Activity activity) {
// // 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;
// }
// }
// return false;
// 經過其餘方式判斷是否有劉海屏 目前官方提供有開發文檔的就 小米,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;
}
}
/**
* 判斷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
*
* @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", 1)) == 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;
}
}
}
複製代碼