-2
我處於奇怪的狀況,我試圖在各處搜索,但是我沒有找到任何有用的東西。可能是我跟隨糟糕的設計。但這裏是我的情況:在菜單項上點擊創建上下文菜單
我在我的應用程序中有AppBar
,我在我們通常所做的應用程序欄上添加了ActionButton
。現在我想在用戶點擊應用欄的任何一個動作按鈕時顯示上下文菜單。
例如:如果我有應用程序欄上的設置按鈕,並且用戶單擊該按鈕,那麼我想顯示具有多個選項的上下文菜單。 我知道如何創建上下文菜單和處理上下文菜單項點擊,但我不知道如何從操作按鈕點擊控制導致顯示ContextMenu
。
這裏是我的代碼:
//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;
}
感謝您的幫助