如何在运行时在ICS中添加/删除菜单项?

时间:2012-01-31 18:25:44

标签: android android-4.0-ice-cream-sandwich

我正在使用SDK附带的ActionBarCompat示例。我的活动过去常常会根据活动的状态改变菜单项。我会在onPrepareOptionsMenu()

中修改菜单
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();

    if (some condition) {
        menu.add(...);
    }
    ...
}

在ICS中,我相信我们应该通过调用invalidateOptionsMenu()修改菜单项,然后再次调用我们的onCreateOptionsMenu()。在这里,我们可以删除菜单项(不添加它们)。那么我们可能最终得到这样的东西:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (SDK >= 11) {
        // full menu is reloaded from xml every time we 
        // called invalidateOptionsMenu(), so we have a
        // fresh menu here again.
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (SDK >= 11) {
        // we can remove items depending on our state, 
        // but not add any, that's ok.
    }
    else {
        // we only have our original menu instance here,
        // so if we previously had called menu.remove(),
        // we need to call menu.add() here:
        if (foo == true) {
            menu.remove(R.id.menu_item_foo);
        } else {
            menu.add(R.id.menu_item_foo, ...);
        }
    }
}

这看起来有点尴尬,

由于

1 个答案:

答案 0 :(得分:1)

演示项目 https://github.com/AlienAsRoger/CourtDeadlines

这就是我解决它的方法:

在actionbarcompat项目的ActionBarHelperBase.java中

...

private View addActionItemCompatFromMenuItem(final MenuItem item) {

final int itemId = item.getItemId();

...

此类的创建者复制了对象的属性,但没有复制item的id,因此以后用fiven id找不到它。

所以我在那个方法中添加了它:

...
actionButton.setId(itemId);
...

和我在同一个班级中使用:

@Override
public void hideMenuItemById(int id, boolean show){
    getActionBarCompat().findViewById(id).setVisibility(show? View.VISIBLE: View.GONE);
}

希望它可以帮到你。

<强>更新

来自ActionBarCompat示例的ActionBarActivity中的

package com.chess.ui.activities;

import actionbarcompat.ActionBarActivity;
import actionbarcompat.ActionBarHelper;
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import com.chess.R;
import com.chess.lcc.android.LccHolder;

public abstract class CoreActivityActionBar2 extends ActionBarActivity {

    protected boolean showActionSearch;
    protected boolean showActionSettings;
    protected boolean showActionNewGame;
    protected boolean showActionRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && getActionBar() != null) {
            getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
                    | ActionBar.DISPLAY_USE_LOGO
                    | ActionBar.DISPLAY_SHOW_HOME
                    | ActionBar.DISPLAY_SHOW_TITLE);
        }
    }

    @Override
    protected void onStart() {
        if (HONEYCOMB_PLUS_API) {
            adjustActionBar();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!HONEYCOMB_PLUS_API) {
            adjustActionBar();
        }
    }


    private void adjustActionBar() {
        getActionBarHelper().showMenuItemById(R.id.menu_settings, showActionSettings);
        getActionBarHelper().showMenuItemById(R.id.menu_new_game, showActionNewGame);
        getActionBarHelper().showMenuItemById(R.id.menu_refresh, showActionRefresh);
        getActionBarHelper().showMenuItemById(R.id.menu_search, showActionSearch);
        getActionBarHelper().showMenuItemById(R.id.menu_singOut, LccHolder.getInstance(this).isConnected());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.sign_out, menu);
        getActionBarHelper().showMenuItemById(R.id.menu_singOut, LccHolder.getInstance(this).isConnected(), menu);
        getActionBarHelper().showMenuItemById(R.id.menu_search, showActionSearch, menu);
        getActionBarHelper().showMenuItemById(R.id.menu_settings, showActionSettings, menu);
        getActionBarHelper().showMenuItemById(R.id.menu_new_game, showActionNewGame, menu);
        getActionBarHelper().showMenuItemById(R.id.menu_refresh, showActionRefresh, menu);

        if (HONEYCOMB_PLUS_API) {
            // Get the SearchView and set the searchable configuration
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                backToHomeActivity();
                break;
            case R.id.menu_settings:
                startActivity(new Intent(this, PreferencesScreenActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public ActionBarHelper provideActionBarHelper() {
        return getActionBarHelper();
    }

}

其中showActionSettings是切换可见性的布尔标志