是否可以将 Android 中的 MenuItem 变灰(不仅仅是禁用)? [英] Is it possible to grey out (not just disable) a MenuItem in Android?

查看:9
本文介绍了是否可以将 Android 中的 MenuItem 变灰(不仅仅是禁用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Blackberry 上的相同功能存在一个问题,并且一些不同的线程提到了这个bug(从那以后据我所知,没有解决方案就关闭了),但我还没有找到专门针对 Android 的.

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 can tell), but I haven't found one specifically for Android.

我根据某种状态在某些 MenuItem 上调用 setEnabled(false),但它们在视觉上看起来是一样的.我希望它们以某种方式被抵消,以便用户知道该选项当前不可用 - 有什么办法可以做到这一点吗?

I'm calling setEnabled(false) on certain MenuItems based on some state, but they visually look the same. I'd like them to be offset in some way, so that the user knows that the option currently isn't available -- is there any way to do that?

推荐答案

我遇到了同样的问题.有两种方法可以让它发挥作用:

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

  1. 将您的图标放在 StateList 中,以便在禁用时使用不同的图标
  2. 我现在用的.在 onPrepareOptionsMenu() 中自己更改图标,如下所示:

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

您可以调用invalidateOptionsMenu()(或来自ABS,supportInvalidateOptionsMenu())来重建菜单.

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

编辑:更新了解决方案 2

来源:https://groups.google.com/论坛/?fromgroups#!topic/actionbarsherlock/Z8Ic8djq-3o

这篇关于是否可以将 Android 中的 MenuItem 变灰(不仅仅是禁用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆