更改Android的点击操作栏菜单项的图标 [英] Change Android Action Bar Menu Item's Icon on Click

查看:184
本文介绍了更改Android的点击操作栏菜单项的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是福尔摩斯行动起来吧。 我有2个项目上的操作栏。当项目选择(活动),我想改变图标的​​形象。

I'm using sherlock action bar. I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.

这是对我的Java code

This is my code on Java

    @Override
    public boolean onPrepareOptionsMenu (Menu menu){
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menutes, menu);
    todaySched=menu.findItem(R.id.todaySched);
    if(todaySched.isEnabled()){
        todaySched.setIcon(R.drawable.calendarselected);

    }
    return true;
}

但是当我做这个图标变成双倍,图标不会改变都不是。 有人可以帮忙吗?

but when I do this the icon become double, and the icon won't change neither. Can someone help?

推荐答案

使用上onOptionsItemSelected方法

Use the on onOptionsItemSelected method

@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.todaySched:

              // put your code here to change the icon
              return true;

          default:
              return super.onOptionsItemSelected(item);
      }
  }

您可能需要包含正确的命名空间的ActionBar福尔摩斯库,以确保它覆盖了正确的菜单项。因此,该方法的开始将是这样的:

you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:

@Override
  public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)

这篇关于更改Android的点击操作栏菜单项的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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