Is it possible to grey out (not just disable) a MenuItem in Android?

后端 未结 8 1112
旧时难觅i
旧时难觅i 2020-12-23 13:35

There\'s a question for the same functionality on Blackberry, and a few different threads referred to this bug (which has since been closed without resolution as far as I ca

相关标签:
8条回答
  • 2020-12-23 13:59

    Here's a simple way to do it (using Kotlin):

    fun changeMenuItemColour(enabled: Boolean) {
        var menuItem = SpannableString(mCustomToolbar?.menu?.findItem(R.id.some_menu_item)?.title)
        var style = activity?.resources?.getColor(R.color.darkGraphite)!!
        if (enabled) style = activity?.resources?.getColor(R.color.black)!!
        menuItem.setSpan(ForegroundColorSpan(style), 0, menuItem.length, 0)
    }
    
    0 讨论(0)
  • 2020-12-23 14:00

    I had the same issue. There are two ways of getting this to work:

    1. Put your icons in a StateList so that a different icon will be used on disable
    2. What I use now. Change the icon yourself with something like this in onPrepareOptionsMenu():

      public boolean onPrepareOptionsMenu(Menu menu) {
          boolean menusEnabled = reachedEndOfSlidehow(); // enable or disable?
          MenuItem item = menu.findItem(R.id.menu_next_slide);
          Drawable resIcon = getResources().getDrawable(R.drawable.ic_next_slide);
      
          if (!menusEnabled)
              resIcon.mutate().setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN);
      
          item.setEnabled(menusEnabled); // any text will be automatically disabled
          item.setIcon(resIcon);
      }
      

    You can call invalidateOptionsMenu() (or from ABS, supportInvalidateOptionsMenu()) to rebuild the menu.

    EDIT: Updated solution 2

    Source: https://groups.google.com/forum/?fromgroups#!topic/actionbarsherlock/Z8Ic8djq-3o

    0 讨论(0)
  • I found a new way to solve this issue using a drawable selector xml file. You just create a selector with the icon you want to use in your menu item, then you can either change the tint, alpha or both of the bitmap:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_enabled="true">
            <bitmap android:src="@drawable/ic_menu_item"
                android:tint="@color/enabled_color"
                android:alpha="@integer/enabled_alpha"/>
        </item>
    
        <item android:state_enabled="false">
            <bitmap android:src="@drawable/ic_menu_item"
                android:tint="@color/disabled_color"
                android:alpha="@integer/disabled_alpha"/>
        </item>
    </selector>
    

    As a side note; I like to set the tint to "?android:attr/textColorPrimary" for enabled state and "?android:attr/textColorHint" for disabled state. This way it will adjust depending on the theme used.


    Then you can just set the icon in your menu xml file to the selector resource:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item android:id="@+id/menu_action"
            android:orderInCategory="0"
            android:title="@string/title_menu_action"
            android:icon="@drawable/ic_menu_item_selector"
            app:showAsAction="ifRoom"/>
    
    </menu>
    

    Then when you call item.setEnabled(enabled) the color and/or alpha of the icon will change along with the state!

    0 讨论(0)
  • 2020-12-23 14:05

    The way I did it is by using "itemIconTint" in NavigationView, you can also grey out the text by using "itemTextColor"

    This is Navigationview:

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemBackground="@color/white"
        android:background="@color/white"
        app:itemTextColor="@color/menu_text_color"
        app:itemIconTint="@color/menu_text_color"
        app:menu="@menu/main_drawer" />
    

    and the "@color/menu_text_color" is a selector:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@color/primaryColor" />
        <item android:state_enabled="false" android:color="@color/disabled_text_color" />
        <item android:color="@color/primaryText" />
    </selector>
    

    Finally, if you want to disable a menuitem,

    MenuItem item = mNavigationView.getMenu().findItem(R.id.your_menu_item);
    item.setEnabled(isEnable);
    

    Done!

    0 讨论(0)
  • 2020-12-23 14:07

    Have a look at this link

    setEnabled can also be used for MenuItems.

    0 讨论(0)
  • 2020-12-23 14:10

    I was having difficulty with this on modern android with MaterialComponents theme. My problem was I had set <item name="actionMenuTextColor">@color/blue</item> in styles.xml and this overrides the text color whether the item is enabled or disabled. The solution is to set a Color state list and not a color directly.

    My styles attribute now looks like:

    <item name="actionMenuTextColor">@color/menu_color_selector</item>

    0 讨论(0)
提交回复
热议问题