如何在膨胀视图上显示上下文菜单?
我想显示膨胀视图的上下文菜单。下面是代码示例:
对于 grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:antialias="true" />
现在我在我的 activiy 类中使用它:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select action");
menu.add(0, 1, 0, "Action1");
menu.add(0, 2, 0, "Action2");
super.onCreateContextMenu(menu, v, menuInfo);
}
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
});
此代码运行时没有任何错误,但当我单击 imageView 时,上下文菜单不会显示。这段代码有什么问题吗?
I want to display contextmenu for an inflated view. Here is the code sample:
for grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:antialias="true" />
Now I am using it in my activiy class as:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select action");
menu.add(0, 1, 0, "Action1");
menu.add(0, 2, 0, "Action2");
super.onCreateContextMenu(menu, v, menuInfo);
}
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.grid_layout, null);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
});
This code runs without any error but the context menu does not show up when I click the imageView. Is there anything wrong with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经找到了针对这种情况的解决方法并解决了我的问题。
正如我所说,如果我在 setContentView() 方法中设置的 XML 中定义 ImageView,则相同的代码可以正常工作并显示 ContextMenu。我只是使用该 imageView 的对象来注册 contextMenu 并在单击膨胀项目时显示 ContextMenu。这是示例代码:
希望这会对某人有所帮助!
I have found a workaround for this situation and solved my problem.
As I told that the same code works fine and displays the ContextMenu, if I define the ImageView in the XML which I set in setContentView() method. I simply used the object of that imageView to register the contextMenu and displayed the ContextMenu on click of inflated item. Here is the sample code:
Hope this will help someone!
添加代码:
之后Imageview将获得clickEvent。
Add in your code:
after that Imageview will get clickEvent.
因为您正在创建一个匿名内部类(通过执行
new View.OnClickListener
),所以您不再在 UI 线程(您的 Activity 类)中进行操作,因此当您想要 <代码>registerForContextMenu 和openContextMenu
。您可以使用Handler
向 UI 线程(您的 Activity 类)发送消息来执行这些操作,或者尝试在内部类中引用您的 Activity 类。像这样的东西:Because you are creating an Anonymous Inner Class (by doing
new View.OnClickListener
) you are no longer operating in the UI thread (your Activity class), so the context menu does not load when you want toregisterForContextMenu
andopenContextMenu
. You can either use aHandler
to send a message to the UI thread(your Activity class) to perform these actions, or try to reference your Activity class in your inner class. Something like this: