Android adding a submenu to a menuItem, where is addSubMenu()?

后端 未结 8 896
情歌与酒
情歌与酒 2020-12-01 04:55

I want to add a submenu inside my OptionsMenu to a menuItem, programatically according to my parameters. I\'ve checked \"MenuItem\" in android sdk and there is no addSubMenu

8条回答
  •  时光取名叫无心
    2020-12-01 05:34

    Sometimes Android weirdness is really amazing (and amusing..). I solved it this way:

    a) Define in XML a submenu placeholder like this:

     
       
        
       
    
    

    b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this:

        public boolean onCreateOptionsMenu(Menu menu) { 
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.mapoptions, menu);
    
                int idx=0;
                SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
                subm.clear(); // delete place holder
    
                while(true)
                {
                    anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
                    if(anarea == null)
                        break;
                    subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
                    ++idx;
                }
    }
    

提交回复
热议问题