我用這個示例代碼爲我的應用程序創建一個工具欄。 https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar如何以編程方式更改android工具欄中的圖標?
我的一個項目是我的應用程序的靜音按鈕。所以圖標是標準的揚聲器圖標。點擊該按鈕後,該按鈕可以靜音或取消靜音。這工作正常,但我需要更改圖標以匹配設置。
我試過下面的代碼,但它不會更改圖標。
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.home, menu);
_menu = menu;
return base.OnCreateOptionsMenu(menu);
}
private void setActionIcon_mute(bool setmuteicon)
{
IMenuItem item = _menu.FindItem(Resource.Id.mmute);
if (_menu != null)
{
if (setmuteicon)
{
//mute it
//this does nothing
item.SetIcon(Resource.Drawable.ic_volume_off_white_24dp);
}
else
{
//unmute it
//this does nothing
item.SetIcon(Resource.Drawable.ic_volume_mute_white_24dp);
}
}
}
有關如何做到這一點的任何想法?
@ user3302938,如果這能夠充分回答您的問題,請將其標記爲答案(我可以使用聲望)。如果您覺得它不能回答您的問題,請發表評論,以便我們深入瞭解問題。謝謝。 – dshrout