如何在膨胀视图上显示上下文菜单?

发布于 2024-10-24 19:31:43 字数 1291 浏览 1 评论 0原文

我想显示膨胀视图的上下文菜单。下面是代码示例:

对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长安忆 2024-10-31 19:31:43

我已经找到了针对这种情况的解决方法并解决了我的问题。
正如我所说,如果我在 setContentView() 方法中设置的 XML 中定义 ImageView,则相同的代码可以正常工作并显示 ContextMenu。我只是使用该 imageView 的对象来注册 contextMenu 并在单击膨胀项目时显示 ContextMenu。这是示例代码:

@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);
    }

    ImageView imageViewInContext = (ImageView) findViewById(R.id.imageview_in_main_xml);
    registerForContextMenu(imageViewInContext);

    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) {                   
                 openContextMenu(imageViewInContext);
            }

        });

希望这会对某人有所帮助!

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:

@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);
    }

    ImageView imageViewInContext = (ImageView) findViewById(R.id.imageview_in_main_xml);
    registerForContextMenu(imageViewInContext);

    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) {                   
                 openContextMenu(imageViewInContext);
            }

        });

Hope this will help someone!

傾旎 2024-10-31 19:31:43

添加代码:

imageView.setFocusable(true);
imageView.setClickable(true);

之后Imageview将获得clickEvent。

Add in your code:

imageView.setFocusable(true);
imageView.setClickable(true);

after that Imageview will get clickEvent.

向地狱狂奔 2024-10-31 19:31:43

因为您正在创建一个匿名内部类(通过执行 new View.OnClickListener),所以您不再在 UI 线程(您的 Activity 类)中进行操作,因此当您想要 <代码>registerForContextMenu 和openContextMenu。您可以使用 Handler 向 UI 线程(您的 Activity 类)发送消息来执行这些操作,或者尝试在内部类中引用您的 Activity 类。像这样的东西:

activityClassName.this.registerForContextMenu(v);
activityClassName.this.openContextMenu(v);

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 to registerForContextMenu and openContextMenu. You can either use a Handler 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:

activityClassName.this.registerForContextMenu(v);
activityClassName.this.openContextMenu(v);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文