【问题标题】:How can I hide the actionbar in a single fragment?如何将操作栏隐藏在单个片段中?
【发布时间】:2014-03-14 20:33:36
【问题描述】:

我在我的应用程序中使用 AppCompat 作为我的主题。另外,我正在使用 viewpager 来管理一些片段。 现在我想将操作栏隐藏在我的一个片段中。我怎样才能做到这一点。我试图像这样在片段中手动隐藏操作栏

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();
}

但这会隐藏所有片段的操作栏。有什么想法吗?

【问题讨论】:

    标签: android android-activity android-actionbar android-appcompat


    【解决方案1】:

    当您加载其他片段调用时(例如在 onResume 中):

    aBar.show();
    

    编辑:

    在您希望显示 ActionBar 的每个片段中,放置以下方法:

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

    在您不希望操作栏显示的 Fragment 的 OnResume 方法中执行以下操作:

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

    既然您使用的是视图寻呼机,我假设您使用的代码类似于:

    http://developer.android.com/reference/android/support/v4/view/ViewPager.html

    在这个方法中是什么情况:

    @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();
                        }
                    }
                }
            }
    

    【讨论】:

    • 我应该在哪里打电话给onResume()。在宿主 actionbaractivity 中还是在其他片段本身中?
    • 嗯,这仍然不起作用。现在操作栏再次显示在所有片段中
    • 很抱歉忘记解决原始片段。立即查看。
    • 仍然没有成功。操作栏仍在显示。必须有办法做到这一点......
    • 等等 - 片段不会同时出现在屏幕上吧?
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多