Android uses popupwindow to implement custom overflow

This example shares the specific code of popupwindow to realize user-defined overflow for your reference. The specific contents are as follows

When the action of the action bar cannot be placed, the system will collect it in overflow.

Use the hierarchy viewer to view the overflow generated by the system and find that it is a popupwindow.

Therefore, we can also use popupwindow to write our own overflow to achieve more functions and achieve the same effect as wechat.

The first time I wrote, there was a lot of nonsense. I hope I can be more forgiving.

Effects (GIF presentation at the bottom of the article):

The action on the far right (the three-point menu) is the action added by yourself, using the icon IC in the Android development package_ action_ Overflow.png, which can be downloaded from the official website.

First, add an action in the item. For demonstration, add a submenu

The listening ID is action_ Overflow action, create popupwindow to pop up your own overflow.

The following describes the popupmyoverflow () method, which pops up our overflow. The layout file of the custom overflow is r.layout.action_ overflow_ Popwindow, I won't post it here.

In Android, in order to adapt to different screen densities and sizes, Android uses DP units, but in Java code, it mostly accepts the size of PX units, so it needs to be converted here.

Method of converting DP to PX.

OK, now we have everything to display the custom overflow! Run your app.

Final effect:

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
分享
二维码
< <上一篇
下一篇>>