显示popupWindow

/**
 * 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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值