【问题标题】:Disable ActionBar Menu Item but still clickable禁用 ActionBar 菜单项但仍可点击
【发布时间】:2013-06-26 02:58:31
【问题描述】:

我想禁用ActionBar Menu Item,但让它可点击。有什么办法可以做到吗?

现在,如果我将setEnabled 设置为false,它只是灰显,这是正常行为,但不可点击。

【问题讨论】:

  • 灰色但不是clickable 是什么意思。如果您拨打setEnabled(false),它将显示为灰色。
  • 是的,我希望它显示为灰色,但我也希望它是可点击的。

标签: android actionbarsherlock android-actionbar


【解决方案1】:

这并不能真正回答问题,因为ActionBar Menu Item 不能同时被禁用和可点击。我最终做的是启用Menu Item 并将文本颜色设置为灰色以使其看起来像伪禁用。如果满足条件,它将更改为默认颜色。

信用:Nicolai Buch Andersen

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    SpannableStringBuilder text = new SpannableStringBuilder();
    text.append(getString(R.string.menu_title));

    if (someCondition){
         text.setSpan(new ForegroundColorSpan(Color.BLACK), 
                 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    else {
        text.setSpan(new ForegroundColorSpan(Color.LTGRAY), 
                0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


    MenuItem item = menu.findItem(R.id.some_menu_id);
    item.setTitle(text);

    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多