public class HeightProvider extends PopupWindow implements OnGlobalLayoutListener {
private Activity mActivity;
private View rootView;
private HeightListener listener;
private int heightMax; // 记录 popup 内容区的最大高度
public HeightProvider(Activity activity) {
super(activity);
this.mActivity = activity;
// 基础配置
rootView = new View(activity);
setContentView(rootView);
// 监听全局 Layout 变化
rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
setBackgroundDrawable(new ColorDrawable(0));
// 设置宽度为 0, 高度为全屏
setWidth(0);
setHeight(LayoutParams.MATCH_PARENT);
// 设置键盘弹出方式
setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
public HeightProvider init() {
if (!isShowing()) {
final View view = mActivity.getWindow().getDecorView();
// 延迟加载 popupwindow, 如果不加延迟就会报错
view.post(new Runnable() {
@Override
public void run() {
showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
}
});
}
return this;
}
public HeightProvider setHeightListener(HeightListener listener) {
this.listener = listener;
return this;
}
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
if (rect.bottom> heightMax) {
heightMax = rect.bottom;
}
// 两者的差值就是键盘的高度
int keyboardHeight = heightMax - rect.bottom;
if (listener != null) {
listener.onHeightChanged(keyboardHeight);
}
}
public interface HeightListener {
void onHeightChanged(int height);
}
}
来源: http://www.jianshu.com/p/ea7fb3387168