在onCreatView判断,是否有虚拟按钮
if (ScreenUtils.isShowNavBar(this)) {
getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, ScreenUtils.getNavigationBarHeight(this));
}
剩余调用下面工具类方法.
/**
* 判断是否显示了导航栏 * (说明这里的context 一定要是activity的context 否则类型转换失败) * * @param context * @return
*/
public static boolean isShowNavBar(Context context) {
if (null == context) {
return false;
}
//获取应用区域高度
Rect outRect1 = new Rect();
try {
((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
} catch (ClassCastException e) {
e.printStackTrace();
return false;
}
//状态栏高度
int activityHeight = outRect1.height();
//屏幕物理高度 减去 状态栏高度
int statusBarHeight = getStatusBarHeight();
//剩余高度跟应用区域高度相等 说明导航栏没有显示 否则相反
int remainHeight = getRealHeight() - statusBarHeight;
if (activityHeight == remainHeight) {
return false;
} else {
return true;
}
}
/**
* 获取状态栏高度 * * @return
*/
private static int getStatusBarHeight() {
int result = 0;
int resourceId = App.getInstance().getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = App.getInstance().getResources().getDimensionPixelSize(resourceId);
}
return result;
}
/**
* 活动屏幕信息
*/
private static WindowManager wm;
/**
* 获取真实屏幕高度 * * @return
*/
@SuppressLint("ObsoleteSdkInt")
private static int getRealHeight() {
if (null == wm) {
wm = (WindowManager) App.getInstance().getSystemService(Context.WINDOW_SERVICE);
}
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.y;
}
/**
* 获取底部导航栏高度
*/
public static int getNavigationBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
//获取NavigationBar的高度
return resources.getDimensionPixelSize(resourceId);
}
总结:华为5.0小屏和华为p20 9.0系统异形屏都可以.