android - 出现软键盘时如何隐藏底部导航菜单?
问题描述
我知道将 adjustPan 设置为隐藏底部导航菜单,但效果会向上推 RecyclerView & Toolbar。我试过设置adjustResize。它工作正常,但底部导航仍然显示。
我想隐藏底部导航菜单,但保留 RecyclerView 和工具栏的 adjustResize 效果。
解决方案
在父视图上使用 ViewTreeObserver
parentView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
Rect r = new Rect();
parentView.getWindowVisibleDisplayFrame(r);
int screenHeight = container.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
// 0.15 ratio is perhaps enough to determine keypad height.
if (keypadHeight > screenHeight * 0.15) {
// keyboard is opened
bottomLayout.setVisibility(View.GONE);
} else {
// keyboard is closed
bottomLayout.post(() -> bottomLayout.setVisibility(View.VISIBLE));
}
});