參考鏈接: 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
下移了一個狀態欄的高度, 所以處理方式是代碼動態的把底部的控件向上偏移狀態欄的高度, 在 Fragment
的 onViewCreated
方法中, 使用:
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);
}