尝试在操作栏上隐藏和显示菜单项 [英] Trying to hide and show menu items on action bar

查看:26
本文介绍了尝试在操作栏上隐藏和显示菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了有关堆栈溢出的问题,但找不到解决方案.

I have looked through the questions on stack overflow and can't find the solution.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.themenu, menu);
    MenuItem   item = menu.findItem(R.id.menu_settings);
    MenuItem item2 = menu.findItem(R.id.menu_save);
    item.setVisible(isdown);
    item2.setVisible(isdown);
    return true;
}

这将我的菜单项设置为可见(item1 和 item2).onclick 工作正常

This sets my menu items to visible (item1 and item2). the onclick works fine

public void inflateTextarea() {
    if(isdown == true) {
        isdown = false;
        LinearLayout tl = (LinearLayout)findViewById(R.id.content);
        tl.setVisibility(View.VISIBLE);
        ScaleAnimation scale =  new ScaleAnimation(1, 1, 0, 1);
        scale.setFillAfter(true);
        scale.setDuration(500);
        tl.startAnimation(scale);
    }
}

然后这将我的 isdown 布尔值设置为 false.在堆栈上,人们说 onPrepareOptionsMenu 应该在我每次点击时触发,但事实并非如此.我可以在 onclick 功能上隐藏一个菜单项

Then this sets my isdown boolean to false. on stack people say that the onPrepareOptionsMenu should fire everytime I click but this is not the case. I am able to hide one menu item on the onclick function

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menu_settings: 
            Log.v("Log:","edit_item pressed");
            return true;
    }
}

但是我有多个菜单项需要隐藏,而其他菜单项我想显示.我该怎么办?

But I have multiple menu items that I need to hide and others that I want to show. how can I go about this?

推荐答案

改变 isDown 变量是不够的.每次要更改可见性时,都必须调用 setVisible() 方法.该方法不仅仅是设置一个布尔值,所以仅仅改变一个布尔值是不行的.

Its not enough to change the isDown variable. You have to call the setVisible() method every time you want to change the visibility. That method does more than just setting a boolean value, so just changing a boolean value will not do.

isDown 值更改为 false 后,您需要调用 invalidateOptionsMenu() 它将通过调用 onPrepareOptionsMenu() 重新启动菜单> 再来一次.

After changing the isDown value to false, you need to call invalidateOptionsMenu() which will re-launch the menu by calling onPrepareOptionsMenu() again.

试试这个使菜单项不可见的代码:

Try this code for making the menu items unvisible:

...
isdown = false;
invalidateOptionsMenu();
...

这篇关于尝试在操作栏上隐藏和显示菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆