我当前的菜单中只有一项,是一只猪的图标。我希望当我点击这只猪时,图标会变成另一张图片,比如一只鸡。我已经阅读了StackOverFlow上的其他论坛,但仍然没有成功。
我知道你不能使用findViewbyId来引用菜单项,但是findItem方法对我来说不起作用,或者至少提示无效。请给予建议。
以下是我的当前代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menuImage"
android:title=""
android:icon="@drawable/pig"
android:orderInCategory="1"
app:showAsAction="always|withText"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menuImage:
Toast.makeText(this, "YOU THINK YOU GOT THIS?", Toast.LENGTH_SHORT).show();
MenuItem changeImage = (MenuItem) findViewById(R.id.menuImage);
changeImage.setIcon(R.drawable.chicken);
return true;
}
return true;
}