When you click a button and popupwindow pops up, the lower-left corner of popupwindow is aligned with the button by default. However, if popupwindow is the same, it will be misplaced, especially on tablets of different sizes, the misplacement error is not generally unreliable, but Android only provides the following methods to set the position of popupwindow.
- Showasdropdown (view anchor, int xoff, int yoff) defines the offset based on the reference point in the lower left corner of anchor.
- Showasdropdown (Android. View. View) uses the lower-left corner of anchor as the reference point and does not offset
- Showatlocation (view parent, int gravity, int X, int y) uses parent as the main container, gravity as the alignment reference point, and defines the offset.
The showasdropdwon method is recommended.
This is to center the popupwindow display, so you need to define the horizontal displacement offset (similar to other positions)
IntXoff = Window. getwidth ()/2-parent.getwidth ()/2; Window. Update (); window. showasdropdown (parent,-Xoff, 0 );
: