先把几个属性讲解一下:
//获取自身的长宽高
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupHeight = view.getMeasuredHeight();
popupWidth = view.getMeasuredWidth();
int[] location = new int[2] ;
view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
location [0]--->x坐标,location [1]--->y坐标---注意:控件的左上角坐标-popupWindos显示方式是
左上角控件的左上角
getLocationOnScreen
getLocationInWindow
getLeft
如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。
getLocationOnScreen
getLocationInWindow
控件的getLeft
在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。
// 设置可以获得焦点 PopupWindow是否具有获取焦点的能力,默认为False。一般来讲是没有用的,因为普通的控件是不需要获取焦点的,而对于EditText则不同,如果不能获取焦点,那么EditText将是无法编辑的
setFocusable(true);
// 设置弹窗内可点击
setTouchable(true);
// 设置弹窗外可点击
//这个函数不只能设置背景……,因为你加上它之后,setOutsideTouchable()才会生效;
//而且,只有加上它之后,PopupWindow才会对手机的返回按钮有响应:即,点击手机返回按钮,可以关闭PopupWindow
setBackgroundDrawable(Drawable background)
setOutsideTouchable(true);
//设置显示动画
setAnimationStyle(R.style.popup_animation);
//设置显示的布局
setContentView(view);
-------------------------------终点设置到控件上方的代码---------------------------------------------------
//获取自身的长宽高
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); (获取要加载控件的宽高)
int popupHeight = view.getMeasuredHeight();
int popupWidth = view.getMeasuredWidth();
//获取需要在其上方显示的控件的位置信息
int[] location = new int[2]; //初始化存放控件坐标的一个数组
imgView.getLocationOnScreen(location); //获取控件坐标(x, y)注意:这里的坐标是控件的左上角popup以此显示
//在控件上方显示
popupWindow.showAtLocation(mView , Gravity.NO_GRAVITY, location[0] +
(mView.getWidth() - mGradeLayout.getMeasuredWidth())/2 ,
location[1] - mGradeLayout.getMeasuredHeight() );