文章标题可能有点水了。
如果你在用PopupWindow,
如果你是自己定义了一个控件里面包含了PopupWindow,
或者你extends 了PopupWindow,
当展示的时候,
PopupWindow只显示了一部分,
另外一部分被遮住了,
这时候你想让弹出窗口往上弹,而不是往下;
而当点击窗口上面部分的时候,往下弹,而不是往上。
那,试试下面的代码。
public void showAsDropDown(View view) {
// step 1
// 取得屏幕高度
VmosoApplication app = (VmosoApplication) context.getApplicationContext();
long totalHeight = app.getScreenHeight();
// step 2
// 取得popup window中要显示的控件的高度
rootView.measure(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
int requiredHeight = rootView.getMeasuredHeight();
// step 3
// 取得点击的控件的位置
int[] viewLocation = new int[2];
view.getLocationInWindow(viewLocation);
int viewY = viewLocation[1];
viewY += view.getMeasuredHeight();
// At least make sure we have this number of pixels to the bottom
// so the UI will not be too crowd
final int LEAST_ROOM = 50;
if ((totalHeight - viewY) <= (requiredHeight + LEAST_ROOM)) {
// Show as drop up
// 0: x
// 1: y
int[] location = new int[2];
view.getLocationOnScreen(location);
popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, location[0], location[1]- requiredHeight);
}
else {
// Show as drop down
popupWindow.showAsDropDown(view);
}
}