首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在actionbar中隐藏一些菜单?

如何在actionbar中隐藏一些菜单?
EN

Stack Overflow用户
提问于 2016-04-04 08:38:55
回答 1查看 50关注 0票数 1

在我的应用程序中,我有将近8个菜单,我有两种类型的用户管理和客户端

对于Admin,我需要显示所有8个manus,对于用户,我需要为用户显示2个菜单。

为此,我给了这个

代码语言:javascript
复制
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.m_mymenu, menu);
    return true;      
}

在我的代码出现在简历之后,我将在下面添加一个

代码语言:javascript
复制
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    SharedPreferences s = getSharedPreferences(my_New_SP, 0);
    HashMap<String, String> map= (HashMap<String, String>) s.getAll();

    int id = item.getItemId();

 if (map.get("usage").equals("Admin"))
 {
    if (id == R.id.abc) {
        Intent mypIntent = new Intent(this, Myp.class);
        startActivity(mypIntent);
        return true;
    }
    .
    .
    .
    .
    else if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }   
}

if (map.get("usage").equals("User"))
{
    if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }
}   
    return super.onOptionsItemSelected(item);
}

因此,在这里的选项菜单中,我只想显示两个用户和8个管理员。

但是当我选择它作为用户时,我能够看到所有的菜单,其中只有两个在工作。所以在这里我只想显示工作菜单应该隐藏起来。

有人能建议我做这种事吗..。

这里的菜单来自android菜单,而不是动态菜单.

EN

回答 1

Stack Overflow用户

发布于 2016-04-04 08:55:47

代码语言:javascript
复制
private Menu menu1;

现在在oncreate中,做这样的事情

代码语言:javascript
复制
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        menu1 = menu.findItem(R.id.web1);
        menu1.setVisible(false);
        return true;
    }

现在,您可以在任何您想要的地方隐藏/显示menu1。对其他选项也这样做

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36397968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档