简体   繁体   English

如何以编程方式更改android工具栏中的图标?

[英]How can I change the icon in an android toolbar programmatically?

I used this sample code to create a toolbar for my app. 我使用此示例代码为我的应用程序创建工具栏。 https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar

One of my items is a mute button for my app. 我的一个项目是我的应用程序的静音按钮。 So the icon is the standard speaker icon. 因此图标是标准扬声器图标。 When the button is clicked, it either mutes or unmutes the app. 单击该按钮时,它会使应用静音或取消静音。 This works just as it should, but I need to change the icon to match the setting. 这样可以正常工作,但我需要更改图标以匹配设置。

I have tried the following code, but it doesn't change the icon. 我尝试了以下代码,但它不会更改图标。

    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);
            }
        }
    }

Any ideas on how to do this? 关于如何做到这一点的任何想法?

The correct way to do this is to override the onPrepareOptionsMenu method in your Activity. 执行此操作的正确方法是覆盖Activity中的onPrepareOptionsMenu方法。

Say you have an app where users can 'Favorite' or 'Un-Favorite' an item. 假设您有一个应用程序,用户可以在其中“收藏”或“取消收藏”项目。 First, create menu.xml with two menu items and set the visible property to true for one and false for the other. 首先,使用两个菜单项创建menu.xml ,并将visible属性设置为true,另一个设置为false。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.myapp.SoloActivity">

    <item android:id="@+id/add_favorite"
        android:title="Favorite"
        android:icon="@drawable/ic_favorite_border_black_24dp"
        android:visible="true"
        app:showAsAction="always" />

    <item android:id="@+id/remove_favorite"
        android:title="Favorite"
        android:icon="@drawable/ic_favorite_black_24dp"
        android:visible="false"
        app:showAsAction="always" />
</menu>

Next, in your Activity, override the onPrepareOptionsMenu method and determine which menu item to show. 接下来,在Activity中,覆盖onPrepareOptionsMenu方法并确定要显示的菜单项。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (IsFavorite) {
        menu.findItem(R.id.add_favorite).setVisible(false);
        menu.findItem(R.id.remove_favorite).setVisible(true)
    } else {
        menu.findItem(R.id.add_favorite).setVisible(true);
        menu.findItem(R.id.remove_favorite).setVisible(false)
    }
}

Now, in the onOptionsItemSelected method of your Activity you can handle the 'click' event for each of these. 现在,在Activity的onOptionsItemSelected方法中,您可以处理每个方法的“click”事件。 Then, to toggle the visiblility of the icons, call invalidateOptionsMenu(); 然后,要切换图标的可见性,请调用invalidateOptionsMenu(); .

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.add_favorite:
            IsFavorite = !IsFavorite;
            Toast.makeText(this, "Added to Favorites", Toast.LENGTH_SHORT).show();
            invalidateOptionsMenu();
            break;

        case R.id.remove_favorite:
            IsFavorite = !IsFavorite;
            Toast.makeText(this, "Removed from Favorites", Toast.LENGTH_SHORT).show();
            invalidateOptionsMenu();
            break;
    }

    return super.onOptionsItemSelected(item);
}

Calling invalidateOptionsMenu(); 调用invalidateOptionsMenu(); will cause the menu to be redrawn, which will cause onPrepareOptionsMenu to be called again. 将导致重绘菜单,这将导致再次调用onPrepareOptionsMenu

This is the recommended way to handle making changes to menu items at runtime. 这是在运行时处理对菜单项进行更改的推荐方法。

Documentation can be found here: https://developer.android.com/guide/topics/ui/menus.html 可以在此处找到文档: https//developer.android.com/guide/topics/ui/menus.html

I hope this helps! 我希望这有帮助!

You can use the toolbar object to get the respective child into a view (ImageView) - View view = toolbar.getChildAt(index); 您可以使用工具栏对象将相应的子项放入视图(ImageView) - View view = toolbar.getChildAt(index); and then use the setImageResource() or any other function to change the icon. 然后使用setImageResource()或任何其他函数来更改图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM