文章说明:本文章核心解决方案来自这里 戳我戳我 ,本文章主要对原方法的小修改,以及使用时遇到的一些问题的解决分享。
/**
* 布局在输入法之上
*
* @param root 最外层布局,需要调整的布局
* @param btmView 最底部的控件
*/
public static ViewTreeObserver.OnGlobalLayoutListener getKeyboardListener(final View root, final View btmView) {
return new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getRootView().getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
//若不可视区域高度大于 h/4,则键盘显示
if (rootInvisibleHeight > root.getRootView().g