Android: dynamically change ActionBar icon?

后端 未结 4 1686
谎友^
谎友^ 2020-12-23 17:37

I would like to dynamically change the \"home\" icon in the ActionBar. This is easily done in v14 with ActionBar.setIcon(...), but I can\'t find anyway to accomplish this i

4条回答
  •  Happy的楠姐
    2020-12-23 17:54

    If your actionbar works like Sherlock and is based on menu items, this is my solution:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem switchButton = menu.findItem(R.id.SwitchSearchOption);     
        if(searchScriptDisplayed){
            switchButton.setIcon(R.drawable.menu_precedent);
        }else{
            switchButton.setIcon(R.drawable.icon_search);
        }
        return super.onPrepareOptionsMenu(menu);
    
    }
    

提交回复
热议问题