Change icons in ActionBar dynamically

后端 未结 6 1974
滥情空心
滥情空心 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:40

    You'll have to save off a reference to the MenuItem after doing the inflation. So something like the following:

    public boolean onCreateOptionsMenu( Menu menu ) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.actionbarlogic, menu );
        playMenu = menu.findItem(R.id.playMenu);
        updatePlayStatus();
        return menu;
    }
    
    public void updatePlayStatus() {
        if( playService.isConnected() ) {
            playService.isPlaying() ? playMenu.setIcon(R.drawable.pause) : playMenu.setIcon(R.drawable.play);
        }
    }
    

    Then you can refer to the playMenu anytime. So you can modify the item as say your player finishes playing and should go back to a play icon.

提交回复
热议问题