简体   繁体   中英

Android popup menu from home screen widget

I want to show a popup menu from a home screen widget like the one in the picture below, but there is a View anchor argument in the PopupMenu constructor method.

But I don't have access to the view in AppWidgetProvider class and only the RemoteViews are available.

So, How can I implement a popup menu in my widget?

Any help would be appreciated. Thanks.

小部件的弹出菜单

You can't use the android.support.v7.widget.PopupMenu directly with RemoteViews . But you are able to copy that behaviour:

  1. Add an anchor view to your RemoteViews layout (this view can be any supported view: https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout )
  2. Create a PendingIntent with a defined intent action and set it with setOnClickPendingIntent for the anchor view
  3. Catch that Intent in onReceive of your AppWidgetProvider by the intent action
  4. Call intent.getSourceBounds() to get the screen position of the clicked anchor view
  5. Start a full transparent Activity and transmit the screen position to that Activity
  6. Create and open a PopupMenu at the transmitted screen position in the transparent Activity
  7. finish() the transparent Activity if the user clicks not on an item of the PopupMenu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM