最近遇到Android 6.0系统横屏时出现按键导航栏遮挡输入法窗口的问题,对比查看华为荣耀V9(Android 8.0)上也有同样的问题,如下图:
可见此问题是Android原生的Bug。其原因是由于导航栏在显示和隐藏的时候没有及时更新layout。
打印其堆栈信息如下:
修改PhoneWindow类的updateNavigationGuard方法,在updateNavigationGuard中调用requestFitSystemWindows方法请求一次新的layout。
文件位置: /frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java
private void updateNavigationGuard(WindowInsets insets) {
// IMEs lay out below the nav bar, but the content view must not (for back compat)
if (getAttributes().type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) {
// prevent the content view from including the nav bar height
if (mContentParent != null) {
if (mContentParent.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams mlp =
(MarginLayoutParams) mContentParent.getLayoutParams();
mlp.bottomMargin = insets.getSystemWindowInsetBottom();
mContentParent.setLayoutParams(mlp);
}
}
// position the navigation guard view, creating it if necessary
if (mNavigationGuard == null) {
mNavigationGuard = new View(mContext);
mNavigationGuard.setBackgroundColor(mContext.getColor(
R.color.input_method_navigation_guard));
addView(mNavigationGuard, indexOfChild(mNavigationColorViewState.view),
new LayoutParams(LayoutParams.MATCH_PARENT,
insets.getSystemWindowInsetBottom(),
Gravity.START | Gravity.BOTTOM));
} else {
LayoutParams lp = (LayoutParams) mNavigationGuard.getLayoutParams();
lp.height = insets.getSystemWindowInsetBottom();
mNavigationGuard.setLayoutParams(lp);
}
//add by pengtg begin.
Log.d(TAG,"updateNavigationGuard+ insets " + insets);
requestFitSystemWindows();
//add by pengtg end.
}
}