2013-08-17 49 views
2

我正在使用sherlock操作栏。 我在操作栏上有2个项目。选择项目(活动)时,我想更改图标的图像。点击更改Android Action Bar菜单项的图标

这是我关于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; 
} 

代码,但是当我这样做双击图标变,图标不会改变都不是。 有人可以帮忙吗?

回答

2

使用上onOptionsItemSelected方法

@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福尔摩斯库正确的命名空间,以保证它覆盖了正确的菜单项。因此,该方法开始看起来就像这样:

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

如果我把它放在onOptionsItemSelected,操作栏不会刷新,所以图标不会改变 –

+1

你延长SherlockActivity或SherlockFragmentActivity ..? ..因为你可以调用这个方法invalidateOptionsMenu() – Daveloper87

+0

SherlockActivity。 我只是再试一次,它的工作原理!谢谢 –