Android 如何从操作栏中的按钮打开下拉菜单

Android 如何从操作栏中的按钮打开下拉菜单,android,button,drop-down-menu,overflow,Android,Button,Drop Down Menu,Overflow,我正在为android开发一个应用程序,它需要在操作栏(靠近溢出)中有一个按钮,该按钮打开一个下拉菜单,作为通过单击溢出按钮弹出的菜单。有什么想法吗?谢谢您可以使用操作栏中的下拉菜单导航来完成此操作,下面是另一个选项(请参见此,希望此选项有帮助:)您可以通过填充相关的膨胀XML文件将项目添加到操作栏中。 在res/menu/action_bar.xml上 <menu xmlns:android="http://schemas.android.com/apk/res/android"

我正在为android开发一个应用程序,它需要在操作栏(靠近溢出)中有一个按钮,该按钮打开一个下拉菜单,作为通过单击溢出按钮弹出的菜单。有什么想法吗?谢谢

您可以使用操作栏中的下拉菜单导航来完成此操作,下面是另一个选项(请参见此,希望此选项有帮助:)

您可以通过填充相关的膨胀XML文件将项目添加到操作栏中。 在res/menu/action_bar.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">
    <item
        android:id="@+id/new_popup"
        android:title="A popup example"
        app:showAsAction="always" // Do not forget to put showAsAction to always so that this item will not be grouped with the overflow
        android:visible="true">
        <menu>
            // Your popup items will be inserted programmatically here by adding item.getSubMenu().add(...) (see the code below)
        </menu>
    </item>
    <item>
        // The overflow items. They all will be hidden here (you put the showAsAction to never)
        ...
    </item>
</menu>
然后,填充菜单上的项目并处理这些项目上的单击事件:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_popup)
{
    item.getSubMenu().add(0, itemId, order, "your text").setIcon(your icon).setOnMenuItemClickListener(new OnMenuItemClickListener() {
 // Handle this item click event
}
    // Add more items!
}


操作栏中的“加号”按钮需要打开一个菜单,该菜单是通过单击溢出按钮弹出的菜单。最终我需要两份菜单

好的,但我想在操作栏中同时使用两个按钮,溢出按钮和我想插入的新按钮。如果我按照您的建议编写代码,我会将溢出按钮替换为新按钮。我已编辑了代码,您可以看到新的弹出项目将添加到何处。在下面的图片中,您有一个“+”项,所有以编程方式插入的项都将添加到“新建弹出”项的子菜单中。其余的将插入溢出组。
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_popup)
{
    item.getSubMenu().add(0, itemId, order, "your text").setIcon(your icon).setOnMenuItemClickListener(new OnMenuItemClickListener() {
 // Handle this item click event
}
    // Add more items!
}