Android: 以编程方式打开菜单

Android: Open menu programmatically

我一直在尝试向我的 android 应用程序添加一项功能,以便在我单击按钮时显示菜单列表:

这是我的代码:

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.MainActivity" >

    <item android:id="@+id/action_onthego_sentence"
          android:title="settings"
          android:orderInCategory="100"
          app:showAsAction="never" />

</menu>

在主要 activity 中,单击按钮后,我会:

        button.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View view )
            {
                runOnUiThread( new Runnable()
                {
                    @Override
                    public void run()
                    {
                        openOptionsMenu();
                    }
                } );
            }
        } );

我需要的是:

如图所示,我想看看菜单是否打开。有什么建议吗?

使用:

MainActivity.this.openOptionsMenu();

然后,检查您的工具栏是否可以。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acercade_activity);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
}

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // MenuInflater inflater = getMenuInflater();
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            //case R.id.action_settings:
            //    return true;

            case R.id.perfil:
                drawerLayout.openDrawer(Gravity.LEFT);
                return true;
            default:
                return super.onOptionsItemSelected(item);



        }

    }

在您的菜单布局中使用它:

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="Setting"
app:showAsAction="ifRoom" />

尝试下面的解决方案它将显示点击按钮菜单列表应该是可见的:

res/menu/main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_changeLang"
        android:orderInCategory="100"
        android:title="Change Lang" />
    <item
        android:id="@+id/action_color"
        android:orderInCategory="100"
        android:title="Select color" />
    <item
        android:id="@+id/action_applist"
        android:orderInCategory="100"
        android:title="App List" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_changeLang:
            // Add your code
            break;
        case R.id.action_color:
            // Add your code
            break;
        case R.id.action_applist:
            // Add your code
            break;
    }
    return super.onOptionsItemSelected(item);
}

尝试使用上述解决方案。它对我有用

您好亲爱的,如果您正在使用以下代码

的工具栏
toolbar.showOverflowMenu();

另外你可以直接调用

MainActivity.this.openOptionsMenu();

如果您在您的应用中使用自定义工具栏,那么以下方法将很有用,

 new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    toolbar.showOverflowMenu();
                }
            }, 500);

使用 Activity.openOptionsMenu() 以编程方式打开菜单,但您可能需要 post 延迟才能调用它。如下图

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        openOptionsMenu();
    }
}, 1000);  

如果需要自动打开子菜单。你可以在openOptionsMenu之后调用menu.performIdentifierAction,如下所示

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        openOptionsMenu();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mainMenu.performIdentifierAction(R.id.submenu, 0);
            }
        }, 500);
    }
}, 1000);  

Android 开发效率工具:https://play.google.com/store/apps/details?id=cn.trinea.android.developertools

Android 开源项目:https://p.codekk.com/