Explain how Android popupwindow can reasonably control the pop-up location (showatlocation)

When it comes to popupwindow, you should feel familiar and easy to use

If the height and width are not specified when creating the popupwindow, showasdropdown will only pop up downward by default. The most obvious disadvantage of this situation is that the pop-up window may be truncated by the screen and the display is incomplete. Therefore, another method showatlocation needs to be used. The coordinates of this method are relative to the whole screen, so we need to calculate the position ourselves.

As shown in the figure below, we can calculate the display position B of popupwindow according to the coordinate a in the upper left corner of the screen, the screen height and width, click the coordinate C in the upper left corner of view, click the size of view and the size of popupwindow layout

The source code of the calculation method is as follows:

Next, call showatloadion to display:

The above example only provides a calculation method, which can be calculated according to requirements in actual development. For example, when anchorview is on the left and in the middle, a popupwindow with adaptive pop-up position can be written according to actual requirements.

Add the code screenutils.java for obtaining the screen height and width:

Demo screenshot:

Demo download address: https://github.com/PopFisher/SmartPopupWindow

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>