全面屏手势导航栏适配
背景
识别是否全面屏手势场景,然后识别手势指示条是否显示,然后再获取指示条高度
由于全面屏手势是Android9开始出现的,所以验证需要从Android9的机型测到最新的15看时机效果。
是否全面屏手势判断
public static boolean isGestureNavMode(Context context) {
if (null == context) {
return false;
}
if (Build.VERSION.SDK_INT >= 31) {
int navMode = Settings.Secure.getInt(context.getContentResolver(), "hide_navigationbar_enable", 0);
return navMode == 2 || navMode == 3;
} else {
return Settings.Secure.getInt(context.getContentResolver(), "navigation_mode", 0) == 2;
}
}
导航栏高度获取
public static int getNavBarHeightRes(Context context) {
int navbarHeight = 0;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navbarHeight = context.getResources().getDimensionPixelSize(resourceId);
}
} else {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object obj = clazz.newInstance();
Field field = clazz.getField("navigation_bar_height");
int height = Integer.parseInt(field.get(obj).toString());
navbarHeight = context.getResources().getDimensionPixelSize(height);
}
} catch (Exception var5) {
Log.w(TAG, "getNavBarHeightRes", var5);
}
Log.d(TAG, "navbarHeight=" + navbarHeight);
return navbarHeight;
}
通过onApplyWindowInsets监听当前的指示条的可见性
mRootLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
boolean isNavBarVisible = insets.isVisible(WindowInsets.Type.navigationBars());
Log.d(TAG, isNavBarVisible + " isNavBarVisible ");
}
return insets;
}
});
是否显示状态栏(顺带mark)
这里的代码片段是在检查Activity的窗口是否设置了FLAG_KEEP_SCREEN_ON标志。
true:全屏,当前activity没有显示状态栏 false:有状态栏
int flag = activity.getWindow().getAttributes().flags;
boolean result = (flag & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0;