我的XML菜单定义将项目www.example.com_refresh的启用状态设置为false。当应用运行时,菜单项呈灰色并被禁用。为什么应用中的此代码不启用该项目?R.id.menu_refresh's enabled state to false. When the app runs the menu item is greyed and disabled. Why is this code in the app not enabling the item?
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem refresh = menu.getItem(R.id.menu_refresh);
refresh.setEnabled(true);
return true;
}
我错过了什么?
3条答案
按热度按时间vkc1a9a21#
尝试使用
menu.findItem()
代替getItem()
。getItem()
从[0,size)获取索引,而findItem()
获取id。ruoxqz4g2#
这是我在菜单处理活动中所做的...
我喜欢这种方法,因为它使代码变得美观和模块化
r55awzrz3#
使用
menu.findItem()
代替getItem()
,因为findItem
用于按ID查找项目。