隐藏 ActionBar 图标

Hide ActionBar icon

我试图在显示抽屉导航时隐藏图标。这是只隐藏设置,而不是我想要的图标。

在我的 MainActivity 上,我看到如果我添加这段代码图标会隐藏,但是如果我按下其他片段来显示它就会崩溃。

    @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    menu.findItem(R.id.ofertasRefresh).setVisible(!drawerOpen); <--- I want to hide this icon
    return super.onPrepareOptionsMenu(menu);
}

当我按下其他片段以显示它时说...:

01-09 20:03:01.493  25761-25761/info.androidhive.slidingmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 25761
java.lang.NullPointerException
        at info.androidhive.slidingmenu.MainActivity.onPrepareOptionsMenu(MainActivity.java:170)
        at android.app.Activity.onPreparePanel(Activity.java:2564)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
        at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
        at com.android.internal.policy.impl.PhoneWindow.run(PhoneWindow.java:221)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5102)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

有什么简单的方法吗? 顺便说一句,在我的片段 1 上,我添加了一个 onCreateOptionsMenu 来显示图像并且效果很好。

 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.refresh_menu, menu);
}

R.menu.refresh_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/ofertasRefresh"
    android:icon="@drawable/ic_action_refresh"
    android:title="Refresh"
    android:alphabeticShortcut="r"
    android:orderInCategory="200"
    android:showAsAction="ifRoom" />
</menu>

干杯。

我认为最好为每个片段和 activity 提供一个菜单 XML。这意味着您不必 hide/show 某些项目。还有关于回调(from the docs):

The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.

Note: Although your fragment receives an on-item-selected callback for each menu item it adds, the activity is first to receive the respective callback when the user selects a menu item. If the activity's implementation of the on-item-selected callback does not handle the selected item, then the event is passed to the fragment's callback. This is true for the Options Menu and context menus.

我认为最好的方法是覆盖 ActionBarDrawerToggle 中的 onDrawerClosed() / onDrawerOpened() 方法。看这里:

public boolean mDrawerOpened;

    ActionBarDrawerToggle abdt = new ActionBarDrawerToggle( ... ) {

        public void onDrawerClosed(View v) {
            super.onDrawerClosed(v);
            mDrawerOpened = false;
            invalidateOptionsMenu();
            syncState();
        }

        public void onDrawerOpened(View v) {
            super.onDrawerClosed(v);
            mDrawerOpened = true;
            invalidateOptionsMenu();
            syncState();
        }

invalidateOptionsMenu() 开始调用主机 Activity 中的 onPrepareOptionsMenu() 方法。那么:

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mDrawerOpened) { menu.removeItem(R.id.ofertasRefresh); }
    if (!mDrawerOpened) { menu.add(Menu.NONE, R.id.ofertasRefresh, Menu.NONE, R.string.title); }
    return super.onPrepareOptionsMenu(menu);
}

使用可见性(可能效果更好):

public MenuItem mi;

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mi == null) { mi = menu.findItem(R.id.ofertasRefresh); }
    if (mDrawerOpened) { mi.setVisible(false); }
    if (!mDrawerOpened) { mi.setVisible(true); }
    return super.onPrepareOptionsMenu(menu);
}