Android 在菜单项上创建上下文菜单单击
我的处境很奇怪,我试着到处找,但没有找到任何有用的东西。可能是我遵循了糟糕的设计。但我的情况是: 我的应用程序中有AppBar,我在AppBar上添加了ActionButton,这是我们通常做的。现在我想在用户单击应用程序栏的任何操作按钮时显示上下文菜单 例如:如果我在应用程序栏上有设置按钮,并且用户单击该按钮,则我希望显示具有多个选项的上下文菜单。 我知道如何创建上下文菜单和处理上下文菜单项的单击,但我不知道如何将控制权从操作按钮单击转移到显示上下文菜单 这是我的密码:Android 在菜单项上创建上下文菜单单击,android,toolbar,appbar,android-contextmenu,Android,Toolbar,Appbar,Android Contextmenu,我的处境很奇怪,我试着到处找,但没有找到任何有用的东西。可能是我遵循了糟糕的设计。但我的情况是: 我的应用程序中有AppBar,我在AppBar上添加了ActionButton,这是我们通常做的。现在我想在用户单击应用程序栏的任何操作按钮时显示上下文菜单 例如:如果我在应用程序栏上有设置按钮,并且用户单击该按钮,则我希望显示具有多个选项的上下文菜单。 我知道如何创建上下文菜单和处理上下文菜单项的单击,但我不知道如何将控制权从操作按钮单击转移到显示上下文菜单 这是我的密码: //inflating
//inflating context menu which will display when user clicks app bar button example like setting
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu_sort, menu);
}
//handling context menu item clicks
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
但我不确定如何处理将显示上下文菜单的应用程序栏按钮点击:
//Below code is to handle app bar item clicks
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//handling the menu clicks on menu.xml
switch (id){
//on below action_add click i want to display context menu
case R.id.action_add:
//not sure what to code here
break;
}
谢谢你的帮助如果我没听懂你的意思,你想显示一个子菜单吗? 然后需要在R.menu.context\u menu\u排序中添加一个菜单标记 像这样:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>
有关更多信息,请参阅