由网友(西瓜和夏天更配哦)分享简介:是否有可能以编程方式更改选项菜单项?任何人都可以向我提供一个例子吗?另外,我想禁用某些项目,使他们不听的点击,这可能吗?Is it possible to change the option menu items programmatically? Can anyone provide me with an exam...
是否有可能以编程方式更改选项菜单项?任何人都可以向我提供一个例子吗?另外,我想禁用某些项目,使他们不听的点击,这可能吗?
Is it possible to change the option menu items programmatically? Can anyone provide me with an example please? Also, I want to disable certain items, so that they don't listen to the clicks, is it possible?
感谢。
推荐答案
对于任何需要动态改变菜单的选项:
For anyone needs to change the options of the menu dynamically:
private Menu menu;
// ...
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.options, menu);
return true;
}
// ...
private void hideOption(int id)
{
MenuItem item = menu.findItem(id);
item.setVisible(false);
}
private void showOption(int id)
{
MenuItem item = menu.findItem(id);
item.setVisible(true);
}
private void setOptionTitle(int id, String title)
{
MenuItem item = menu.findItem(id);
item.setTitle(title);
}
private void setOptionIcon(int id, int iconRes)
{
MenuItem item = menu.findItem(id);
item.setIcon(iconRes);
}
相关推荐
最新文章