How does popupWindow.showAtLocation work? copy from stackoverflow
2014-11-21 15:22
519 查看
After hacking for a few hours trying some black magic maths to calculate centers and try to align the view using Gravity.TOP I found a post that used Gravity.CENTER. I'm collecting my findings here in the hopes it saves someone else some pain.
The view is only needed for the window token, it has no other impact on the location.
Gravity tells the layout manager where to start the coordinate system and how to treat those coordinates. I can't find the docs but hacking is showing me that:
CENTER uses the middle of the popup to be aligned to the x,y specified. So 0,0 is screen centered, with no adjustments for the size of the notification bar.
BOTTOM uses the bottom of the popup to be aligned to the x,y specified. So 0,0 has the popup bottom aligned with the screen bottom. If you want 10px padding then y=10 (not -10) to move the popup up the screen 10 pixels.
TOP uses the top of the popup to be aligned to the x,y specified. So 0,0 has the popup top aligned with the screen top. If you want 10px padding then y=10. NOTE If
you are not in full screen mode then you must also make adjustments for the notification bar.
Gravity.LEFT and Gravity.RIGHT should be obvious now, for my example images they are too big to fit on the screen so they are clamped to the screen size minus the padding I am using.
popupWindow.showAtLocation(anyViewOnlyNeededForWindowToken, Gravity.CENTER, 0, 0);
The view is only needed for the window token, it has no other impact on the location.
Gravity tells the layout manager where to start the coordinate system and how to treat those coordinates. I can't find the docs but hacking is showing me that:
CENTER uses the middle of the popup to be aligned to the x,y specified. So 0,0 is screen centered, with no adjustments for the size of the notification bar.
BOTTOM uses the bottom of the popup to be aligned to the x,y specified. So 0,0 has the popup bottom aligned with the screen bottom. If you want 10px padding then y=10 (not -10) to move the popup up the screen 10 pixels.
TOP uses the top of the popup to be aligned to the x,y specified. So 0,0 has the popup top aligned with the screen top. If you want 10px padding then y=10. NOTE If
you are not in full screen mode then you must also make adjustments for the notification bar.
Gravity.LEFT and Gravity.RIGHT should be obvious now, for my example images they are too big to fit on the screen so they are clamped to the screen size minus the padding I am using.
相关文章推荐
- PopupWindow popupWindow.showAtLocation(view,Gravity.TOP,x,y) 被顶部系统状态栏挡住内容
- Android编程之PopupWindow隐藏及显示方法示例(showAtLocation,showAsDropDown)
- PopupWindow.showAtLocation理解
- (原创)关于PopupWindow的showAsDropDown()和showAtLocation()使用方式及其区别
- 【Android 非常基础】PopupWindow showAtLocation和showAsDropDown参数分析
- (转)关于PopupWindow的showAsDropDown()和showAtLocation()使用方式及其区别
- Android PopupWindow怎么合理控制弹出位置(showAtLocation)
- PopupWindow之showAtLocation,showAsDropDown
- PopupWindow showAtLocation和showAsDropDown参数分析
- 【Android 非常基础】PopupWindow showAtLocation和showAsDropDown参数分析
- PopupWindow showAtLocation和showAsDropDown参数分析
- How does Google Map My Location work?
- zz from stackoverflow - effecient tip: how switch tabs in Eclipse
- How to bind a GridView to a list of multiple types? NHibernate proxy causing problems with databinding [From stack overflow]
- How to Answer(From Stack OverFlow)
- How do I size a UITextView to its content? from stack overflow!
- 转载:How does Google Map My Location work?
- js closure ,copy from stackoverflow
- How does Google Map My Location work?(转载)
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)