Android PopupWindow控件是开发中常见的效果,平时并没有太留意,
遇到了自己稍微封装了一下,实现Android PopupWindow显示在控件上方或者下方
主要代码如下
View view = mInflater.inflate(R.layout.layout_popupwindow, null);
PopUpwindowLayout popUpwindowLayout = (PopUpwindowLayout) view.findViewById(R.id.llayout_popupwindow);
popUpwindowLayout.initViews(mContext, titles, false);
final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int popupWidth = view.getMeasuredWidth();
int popupHeight = view.getMeasuredHeight();
int[] location = new int[2];
// 允许点击外部消失
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// 获得位置
v.getLocationOnScreen(location);
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
附图片和Demo下载Demo
本文介绍了如何在Android开发中使用PopupWindow控件,并展示了如何实现将其显示在指定控件上方或下方的代码示例,包括初始化视图、设置PopupWindow属性、计算位置和显示位置的方法。

2129

被折叠的 条评论
为什么被折叠?



