显示popupWindow
发布日期:2021-06-30 18:38:15 浏览次数:2 分类:技术文章

本文共 1547 字,大约阅读时间需要 5 分钟。

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

转载地址:https://linqiarui.blog.csdn.net/article/details/50764573 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:手势识别器
下一篇:卸载程序

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年05月02日 04时25分56秒