Menu in Action bar Android

2019-02-20 17:21发布

问题:

I have created ActionBar with some number of tabs dynamically like the below code.

public void addTabBar(Context context)
{       
    sActiveContext=context;      
    sActionBar = getActionBar(); 
    sActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}  

public void addTabItem(final String url, String tabTitle)
{   
    arrayList.add(url);
    Tab tab = sActionBar.newTab();
    if(tabTitle.equals(""))
    {
        int childcount=sActionBar.getTabCount();
        tabTitle="Tab" + String.valueOf(childcount+1);          
    }
    tab.setText(tabTitle);          
    tab.setTabListener(this);
    sActionBar.addTab(tab);        
}

  @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        linearLayout=new LinearLayout(sActiveContext);      
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
        linearLayout.setOrientation(LinearLayout.VERTICAL); 
        setContentView(linearLayout);
        CustomWebView webview=new CustomWebView(sActiveContext);
        FrameLayout layout=webview.createwebview();

        for (int i = 0; i < arrayList.size(); i++) {
            if(tab.getPosition()==i)
            {
                webview.initwebview(arrayList.get(i));
                break;
            }
        }
    linearLayout.addView(layout);
    }

If I have converted this code as library and call those methods and I can create n number of tabs in action bar. Now, I wish to add menu items and dropdown menu(ellipse with three dots) like in the link below. http://developer.android.com/guide/topics/ui/actionbar.html#Tabs If I am passing the image to the showMenu(R.drawable.menu_image) method, the menu items have to be dynamically created. How can I achieve this? Please give some suggestions.

回答1:

Try this, In your Activity,

private Menu menu=null;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   this.menu=menu;
   //  call here as, showMenu(R.drawable.menu_image,2222);
   return true;
}
private void showMenu(int imageIcon,int id)//Where imageIcon is R.drawable.menu_image
{
   menu.add(0, id, 0, actionString).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
   MenuItem item=menu.findItem(id);
   item.setIcon(imageIcon);
}


回答2:

try this for handle all menu's for activity, and mange all menu's for fragment transaction on tab change,

private Menu menu=null;

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   this.menu=menu;
   //  call here as, showMenu(R.drawable.menu_image,2222);
   // add all menu's when that needed in your activity like save,cancel,choose color,  //close etc.
   menuHandler(0);
   return true;
}
private void showMenu(int imageIcon,int id)//Where imageIcon is R.drawable.menu_image
{
   menu.add(0, id, 0, actionString).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
   MenuItem item=menu.findItem(id);
   item.setIcon(imageIcon);
}
    private menuHandler(int position)
{
if(position==0)//For tab 1
{
if(!menu.findItem(1111).isVisible())    //check menu item with id 1111 is visible
    menu.findItem(1111).setVisible(true);
if(menu.findItem(2222).isVisible()) 
    menu.findItem(2222).setVisible(false);
}
else if(position==0)
{
if(menu.findItem(1111).isVisible()) 
    menu.findItem(1111).setVisible(false);
if(menu.findItem(2222).isVisible())
    menu.findItem(2222).setVisible(false);
}
else
{
if(menu.findItem(3333)
        menu.findItem(3333).setVisible(false);
if(!menu.findItem(2222).isVisible())
    menu.findItem(2222).setVisible(true);
}
}
@Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) 
    {}
    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) 
    {
menuHandler(tab.getPosition());
}