在popupwindow自定义类的内部是不存在有效的方法去更新位置的,翻阅资料之后发现可以在调用popupwindow的Activity中通过监听根布局的高度来判断输入法是否显示。
public void inputListener(PopWindow popWindow,View view){
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
int screenHeight = rootView.getRootView().getHeight();
int keyboardHeight = screenHeight - rect.bottom;
if (keyboardHeight > 100) {
// 输入法显示
popWindow.update(view, 0, 200, popWindow.getWidth(), popWindow.getHeight());
}else {
// 输入法隐藏
popWindow.update(view, 0, 112, popWindow.getWidth(), popWindow.getHeight());
}
}
});
}
view为popupwindow显示的锚点view,初始化完毕popupwindow的位置后,可以使用update方法来动态更新位置,但是需要注意实际参数需要自己实验。