简体   繁体   English

主屏幕小部件中的Android弹出菜单

[英]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: 但是您可以复制该行为:

  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 ) 将锚点视图添加到RemoteViews布局(此视图可以是任何受支持的视图: 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 使用定义的intent操作创建PendingIntent ,并使用setOnClickPendingIntent设置锚视图
  3. Catch that Intent in onReceive of your AppWidgetProvider by the intent action 通过intent操作在onReceiveAppWidgetProvider捕获该Intent
  4. Call intent.getSourceBounds() to get the screen position of the clicked anchor view 调用intent.getSourceBounds()以获取单击的锚点视图的屏幕位置
  5. Start a full transparent Activity and transmit the screen position to that Activity 启动一个完全透明的Activity ,并将屏幕位置传输到该Activity
  6. Create and open a PopupMenu at the transmitted screen position in the transparent Activity 在透明Activity的传输屏幕位置创建并打开PopupMenu
  7. 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.

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