我怎样才能设置一个动作条项目的不同片段的知名度 [英] How can I set the visibility of an ActionBar item in different fragment

查看:100
本文介绍了我怎样才能设置一个动作条项目的不同片段的知名度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来设置的能见度菜单项充气我MainActivity取决于在其中片段我在

I'm looking for a way to set the visibility of a MenuItem inflated in my MainActivity depending on which Fragment I am on.

有关信息:我使用 actionBarSherlock zxing ,有的 Google服务

For information: I'm using actionBarSherlock, zxing, and some google services.

应用程序内建一个抽屉式导航(带ABS),我也操作 FragmentStack 以这种方式,我每次我切换到另一个片段时,我preSS触摸背我回来在我的主要片段

The application was built with a Navigation drawer(With abs), also I manipulate the FragmentStack in such way I everytime I switch to another Fragment when I press the touch back I come back in my Main Fragment.

下面我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/button_generator" android:title="GENERER" android:icon="@drawable/ic_drawer"></item>
</menu>

下面是我吹菜单:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.d(TAG, "================= onCreateOptionsMenu ================= fragSt: " + fragmentStatus);
    this.getSherlock().getMenuInflater().inflate(R.menu.main, menu);

    mGenQrFromContacts = menu.findItem(R.id.button_generator);


    return true;
}

我已经试过了解决方案意<一href="http://stackoverflow.com/questions/14407667/android-changing-visibility-on-menu-items-with-changing-fragment">here,但在我的情况下工作。

I've tried the solution purposed here, but ain't work in my case.

推荐答案

您应该尝试在你的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // ...  
    // call the method setHasOptionsMenu, to have access to the menu from your fragment
    setHasOptionsMenu(true);

    //...
}

// the create options menu with a MenuInflater to have the menu from your fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.findItem(R.id.button_generator).setVisible(true);
    super.onCreateOptionsMenu(menu, inflater);
}  

而这一点,在你的活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.my_layout, menu);
    menu.findItem(R.id.button_generator).setVisible(false);
    return true;
}

希望这有助于。

Hope this helps.

这篇关于我怎样才能设置一个动作条项目的不同片段的知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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