更改Android的点击操作栏菜单项的图标图标、菜单项、操作、Android

2023-09-12 03:19:56 作者:跑调情歌

我使用的是福尔摩斯行动起来吧。 我有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)