Change icons in ActionBar dynamically

后端 未结 6 1978
滥情空心
滥情空心 2020-12-04 10:58

I have an Activity which has an ActionBar but I need to change the icons on the ActionBar dynamically, I have a pause and a <

6条回答
  •  不知归路
    2020-12-04 11:49

    private Menu mMenu;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity, menu);
    
        // Save the menu reference
        mMenu = menu;
        return super.onCreateOptionsMenu(menu);
    }
    
    // For example - Call when you need to change icon
    private void setActionIcon(boolean showWithBadge)
    {
        MenuItem item = mMenu.findItem(R.id.ITEM_ID);
    
        if(mMenu != null)
        {
            if(showWithBadge)
            {
                item.setIcon(R.drawable.IC_WITH_BADGE);           
            }
            else 
            {
                item.setIcon(R.drawable.IC_WITHOUT_BADGE);
            }
        }
    }
    

提交回复
热议问题