How to hide action bar for fragment?

2019-01-22 11:53发布

How can I hide action bar for certain fragment? I have searched for the answer at stackoverflow, but I have only found a solution, which involves disabling action bar for main activity in android manifest. Since I need to disable action bar for one fragment, this is not an option. Any ideas? Thanks.

EDIT: min API level is 7, sherlock is not being used

7条回答
地球回转人心会变
2楼-- · 2019-01-22 12:27

Put getSupportActionBar().hide() before setContentView in the Activity that holds the fragments.

Also add this: ((AppCompatActivity) getActivity()).getSupportActionBar().hide() in the fragments before inflating layout. This works if you are using this ActionBarActivity.It also removes my lags in hiding action bar

查看更多
ら.Afraid
3楼-- · 2019-01-22 12:30

Hide actionBar using this in the required fragment.

getActivity().getSupportActionBar().hide();

And Show actionBar with this in your next fragment.

getActivity().getActionBar().show();
查看更多
男人必须洒脱
4楼-- · 2019-01-22 12:33

This solution is for complex non-AppCompat applications that use native ToolBar when running Lollipop onwards and native ActionBar otherwise.

It assumes you want to hide the ActionBar whenever Fragments are visible.

Inside onCreate() in each of your Activities:

getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() 
    {
        @Override
        public void onBackStackChanged() {
            U.ABkk(this, getFragmentManager().getBackStackEntryCount());
        }
    }
);

OR (much better) inside a 'singleton' class that implements Application.ActivityLifecycleCallbacks

@Override
public void onActivityCreated(final Activity A, Bundle savedInstanceState) {
    A.getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            U.ABkk(A, A.getFragmentManager().getBackStackEntryCount());
        }
    });
}

Inside the utility class:

/** Show/hide ActionBar for  KitKat devices */
public static void ABkk(Activity A, int count) {
    if (lollipop()) return;     // No problem when using Toolbar
    ActionBar ab = A.getActionBar();
    if (ab==null) return;
    if (count==1) { ab.hide(); }
    if (count==0) { ab.show(); }
}

/** Return true if API 21 or greater */
private static boolean lollipop() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}   

Using onActivityCreated() is a solution that requires no changes to your Fragments or Activities!

查看更多
姐就是有狂的资本
5楼-- · 2019-01-22 12:37

Have you tried getActivity().getSupportActionBar().hide() in the onCreate() of the fragment you wish the ActionBar to be hidden in?

I am assuming you are not using ActionBarSherlock.

查看更多
别忘想泡老子
6楼-- · 2019-01-22 12:38

You can simply put this into your Fragment Class createView method:-

    View layout = inflater.inflate(R.layout.player_fragment, container, false);
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
查看更多
smile是对你的礼貌
7楼-- · 2019-01-22 12:40

getActionBar().hide() or getSupportActionBar().hide() (if using ActionBarCompat v7 lib). Regards

查看更多
登录 后发表回答