Android 菜单项的registerForContextMenu

Android 菜单项的registerForContextMenu,android,android-actionbar,contextmenu,Android,Android Actionbar,Contextmenu,好的,我肯定这是个愚蠢的问题,但我在网上找不到答案。我想为上下文菜单注册一个菜单项,但我不知道如何注册,也不知道如何将菜单项作为视图访问。因此,当我单击应用程序操作栏上的一个按钮时,我希望弹出一个上下文菜单。我猜这必须在OnCreateOptions菜单中完成 编辑:更新。。。添加此代码部分有效,但会覆盖我的Drawable XML 您好,请点击下面的链接 更改注册表ForContextMenuList;注册ForContextMenuButtonName 我希望它对您有用。在resource/

好的,我肯定这是个愚蠢的问题,但我在网上找不到答案。我想为上下文菜单注册一个菜单项,但我不知道如何注册,也不知道如何将菜单项作为视图访问。因此,当我单击应用程序操作栏上的一个按钮时,我希望弹出一个上下文菜单。我猜这必须在OnCreateOptions菜单中完成

编辑:更新。。。添加此代码部分有效,但会覆盖我的Drawable

XML

您好,请点击下面的链接

更改注册表ForContextMenuList;注册ForContextMenuButtonName


我希望它对您有用。

在resource/menu/main.xml中添加以下代码:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >         
                 <menu>
                <item
                    android:id="@+id/gray"
                    android:title="@string/gray" />
                <item
                    android:id="@+id/green"                      
                    android:title="@string/green" />
                <item
                    android:id="@+id/red"                      
                    android:title="@string/red" />
                <item
                    android:id="@+id/orange"                       
                    android:title="@string/orange" />           

                <item
                    android:id="@+id/purple"
                    android:title="@string/dark_blue" />
            </menu>

                </item>
        </menu>

以下是我所做的:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // "menu_main" is the menubar of my actionbar menu
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



public boolean onOptionsItemSelected(MenuItem item) {

    // "item" is the menu button I have pressed
    int id = item.getItemId();

    // "Settings" button
    if (id == R.id.action_settings) {
        return true;
    }

    // Difficulty button to change the difficulty of my game
    else if (id == R.id.action_difficulty) {
        View view = findViewById(R.id.action_difficulty);
        registerForContextMenu(view);
        openContextMenu(view);
    }
    return super.onOptionsItemSelected(item);
}
这对我来说很好

但是!如果菜单栏按钮位于三点按钮后面,则该行

registerForContextMenu(view);

将使您的应用程序崩溃。我正在弄清楚原因。

不太清楚。我想找到它在哪里提到了ActionBar中的某个项目。你在回答我的问题吗?我不明白你的意思是什么?是的,回答你的问题n..当我单击应用程序操作栏上的一个按钮时,我希望弹出一个上下文菜单…在此颜色列表中,颜色将显示为一个子菜单项。我刚刚运行了你的代码,但未能实现这一点。我注意到的是,当你点击ActionBar上的一个特定按钮时,它会改变菜单项的颜色和名称。registerForContextMenu通常不适用于ActionBar按钮。它用于视图ListView、GridView等。。。。请参阅文档:
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >         
                 <menu>
                <item
                    android:id="@+id/gray"
                    android:title="@string/gray" />
                <item
                    android:id="@+id/green"                      
                    android:title="@string/green" />
                <item
                    android:id="@+id/red"                      
                    android:title="@string/red" />
                <item
                    android:id="@+id/orange"                       
                    android:title="@string/orange" />           

                <item
                    android:id="@+id/purple"
                    android:title="@string/dark_blue" />
            </menu>

                </item>
        </menu>
@Override
    public boolean onCreateOptionsMenu(Menu menu) {    
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {           
        switch (item.getItemId()) {   
                    case R.id.gray:
            color = Color.parseColor("#FF666666");              
            return true;

        case R.id.green:
            color = Color.parseColor("#FF96AA39");              
            return true;

        case R.id.orange:
            color = Color.parseColor("#FFF4842D");              
            return true;

        case R.id.purple:
            color = Color.parseColor("#FF5161BC");              
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // "menu_main" is the menubar of my actionbar menu
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



public boolean onOptionsItemSelected(MenuItem item) {

    // "item" is the menu button I have pressed
    int id = item.getItemId();

    // "Settings" button
    if (id == R.id.action_settings) {
        return true;
    }

    // Difficulty button to change the difficulty of my game
    else if (id == R.id.action_difficulty) {
        View view = findViewById(R.id.action_difficulty);
        registerForContextMenu(view);
        openContextMenu(view);
    }
    return super.onOptionsItemSelected(item);
}
registerForContextMenu(view);