当出现软键盘时,如何隐藏“底部导航”菜单?

时间:2019-08-31 10:19:45

标签: android bottomnavigationview

我知道将adjustPan设置为隐藏“底部导航菜单”,但是效果将向上推RecyclerView和工具栏。我尝试设置AdjustResize。它工作正常,但仍显示底部导航。

我想隐藏“底部导航”菜单,但保持RecyclerView和工具栏的AdjustResize效果。

1 个答案:

答案 0 :(得分:1)

在父视图上使用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));
        }
    });