从操作栏菜单选项中打开浮动上下文菜单

时间:2015-04-13 23:47:59

标签: android android-layout android-activity android-actionbar android-menu

我想创建一个浮动上下文菜单,显示用户何时点击操作栏中的特定选项。这是我在白板上制作的图表。

enter image description here

我没有时间使用模型工具。我是android的初学者,但我正在做一些关于它的研究。但是,我刚刚提出了我的行动吧。

enter image description here

我发现很多教程都很复杂,我无法遵循它们。 开始编码此功能的好方法是什么?

1 个答案:

答案 0 :(得分:2)

无论如何我都在发布解决方案:)

请参阅给定链接AlertDialogs

中的列表添加到警告对话框中 menu.xml 中的

添加以下内容:

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

<item
    android:id="@+id/menu_item"
    android:icon="@drawable/ic_launcher"
    android:title="menu_item"
    app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/menu1"
            android:title="menu1"/>
        <item
            android:id="@+id/menu2"
            android:title="menu2"/>
    </menu>
</item>

和活动onOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    ...
    if (item.getItemId() == R.id.menu2)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Submenu").setItems(new String[]
        {
        "Item1", "Item2", "Item3"
        }, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {//TODO
            }
        });
        builder.show();
    }
    ...

    return super.onOptionsItemSelected(item);
}

这将解决目的!