/**
* ivOption点击后显示popupWindow
*
* @param ivOption
*/
protected void showPopupWindow(View ivOption) {
View itemView = (View) ivOption.getParent();// 得到ivOption的父控件
if (mIvOptionPopupWindow == null) {
View contentView = View.inflate(this, R.layout.file_item_pop, null);
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = itemView.getHeight();
mIvOptionPopupWindow = new PopupWindow(contentView, width, height,
true);
}
// 点击popupwindow范围以外的地方时隐藏
mIvOptionPopupWindow.setBackgroundDrawable(new BitmapDrawable());
mIvOptionPopupWindow.setOutsideTouchable(true);
// 控制它放置的位置
if (isShowBottom(itemView)) {// 显示popupwindow在itemView的下方,偏移量都为0
mIvOptionPopupWindow.showAsDropDown(itemView, 0, 0);
} else {// 显示popupwindow在itemView的上方,偏移量y都为-2*itemView.getHeight()
mIvOptionPopupWindow.showAsDropDown(itemView, 0,
-2 * itemView.getHeight());
}
}
/**
* 判断popupWindow是否显示在条目的下方
*
* @param itemView
* @return
*/
private boolean isShowBottom(View itemView) {
// 得到屏幕的高度
// int heightPixels =
// getResources().getDisplayMetrics().heightPixels;//方式1
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();// 方式2
int[] location = new int[2];
// location[0]-->x
// location[1]-->y
itemView.getLocationInWindow(location);
// 得到itemView在屏幕中Y轴的值
int itemViewY = location[1];
// 得到itemView距离屏幕底部的距离
int distance = screenHeight - itemViewY - itemView.getHeight();
if (distance < itemView.getHeight()) {// 条目下方放不下popupWindow
return false;
} else {// 条目下方放得下popupWindow
return true;
}
}
//让popupWindow消失
mIvOptionPopupWindow.dismiss();
显示popupWindow
最新推荐文章于 2022-11-17 11:15:06 发布