如何隐藏在一个片段中的动作条?片段、动作

2023-09-12 03:29:02 作者:# 1線之間的懸念

我使用AppCompat我在我的应用程序的主题。另外,我使用的是viewpager管理一些片段。 现在,我想隐藏动作条在我的片段之一。我怎样才能做到这一点。我试图在片段像这样

I am using AppCompat for my theme in my app. Also, I am using a viewpager to manage some fragments. Now I would to hide the actionbar in one of my fragments. How can I do that. I tried to hide the actionbar manually in the fragment like this

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBarActivity activity = (ActionBarActivity) this.getActivity();
    ActionBar aBar = activity.getSupportActionBar();
    aBar.hide();
}

但这隐藏动作条为所有片段。任何想法?

推荐答案

在加载其他片段致电(例如在onResume):

When you load other fragments call (for example in onResume):

aBar.show();

编辑:

在你想要有演出的每个片段中的动作条把下面的方法:

in each fragment that you want to have show the ActionBar put the following method:

@Override
public void onResume() {
    super.onResume();
    aBar.show();
}

在片段中您不想要的操作栏显示OnResume方法做到这一点:

in the OnResume method of the Fragment which you don't want the action bar to show do this:

@Override
public void onResume() {
    super.onResume();
    aBar.hide();
}

由于您使用的视图寻呼机我假设你正在使用code类似于:

SInce you're using the view pager I assume you're using code similar to:

http://developer.android.com/reference/安卓/支持/ V4 /视图/ ViewPager.html

在这种情况下,在该方法中:

In which case in this method:

@Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                    // add check here to determine if the selected item is the fragment you want     to hide the action bar in... 
                    // if so call aBar.hide();
                      // for example:

                    if(i == MY_NO_BAR_FRAGMENT) {
                          // when you set up the view fragment, you would set MY_NO_BAR_FRAGMENT to the index of that fragment in the view pagers list of pages.
                          aBar.hide();
                    }
                }
            }
        }