[英]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. 我想从主屏幕小部件显示一个弹出菜单,如下面的图片所示,但是PopupMenu构造函数中有一个View anchor
参数。
But I don't have access to the view in AppWidgetProvider
class and only the RemoteViews are available. 但是我无权访问AppWidgetProvider
类中的视图,并且只有RemoteViews可用。
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
. 您不能将android.support.v7.widget.PopupMenu
直接用于RemoteViews
。 But you are able to copy that behaviour: 但是您可以复制该行为:
PendingIntent
with a defined intent action and set it with setOnClickPendingIntent
for the anchor view 使用定义的intent操作创建PendingIntent
,并使用setOnClickPendingIntent
设置锚视图 Intent
in onReceive
of your AppWidgetProvider
by the intent action 通过intent操作在onReceive
的AppWidgetProvider
捕获该Intent
intent.getSourceBounds()
to get the screen position of the clicked anchor view 调用intent.getSourceBounds()
以获取单击的锚点视图的屏幕位置 Activity
and transmit the screen position to that Activity
启动一个完全透明的Activity
,并将屏幕位置传输到该Activity
PopupMenu
at the transmitted screen position in the transparent Activity
在透明Activity
的传输屏幕位置创建并打开PopupMenu
finish()
the transparent Activity
if the user clicks not on an item of the PopupMenu
如果用户未单击PopupMenu
某个项目,则finish()
透明的Activity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.