gpt4 book ai didi

android - 如何获取Android键盘的高度?

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:32 26 4
gpt4 key购买 nike

如何获取Android键盘的高度?

我尝试:

KeyboardView keyboardView = new KeyboardView(_activity.getApplicationContext(), null);
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getHeight());
Log.i("","xxx height " + keyboardCustom.mKeyboardView.getBottom());

但总是得到0。

最佳答案

使用 OnGlobalLayoutListener 获取键盘高度或实现上面的代码 fragment

  • chatRootLayout 是您的 xml 根布局
  • 将此 rootLayout 作为 parentLayout 参数传递给 checkKeyboardHeight

     private void checkKeyboardHeight(final View parentLayout)
    {
    chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
    @Override
    public void onGlobalLayout()
    {
    Rect r = new Rect();

    chatRootLayout.getWindowVisibleDisplayFrame(r);

    int screenHeight = chatRootLayout.getRootView().getHeight();
    int keyboardHeight = screenHeight - (r.bottom);

    if (previousHeightDiffrence - keyboardHeight > 50)
    {
    // Do some stuff here
    }

    previousHeightDiffrence = keyboardHeight;
    if (keyboardHeight> 100)
    {
    isKeyBoardVisible = true;
    changeKeyboardHeight(keyboardHeight);
    }
    else
    {
    isKeyBoardVisible = false;
    }
    }
    });
    }

    这里是 changeKeyboardHeight() 方法

    private void changeKeyboardHeight(int height) 
    {
    if (height > 100)
    {
    keyboardHeight = height;
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
    yourLayout.setLayoutParams(params);
    }
    }

关于android - 如何获取Android键盘的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com