Android 如何隐藏片段的操作栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21504088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:41:14  来源:igfitidea点击:

How to hide action bar for fragment?

androidandroid-actionbar

提问by user3178137

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.

如何隐藏某些片段的操作栏?我在 stackoverflow 上搜索了答案,但我只找到了一个解决方案,它涉及禁用 android manifest 中主要活动的操作栏。由于我需要为一个片段禁用操作栏,因此这不是一种选择。有任何想法吗?谢谢。

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

编辑:最低 API 级别为 7,未使用 Sherlock

回答by Gober

If you are using AppCompatActivity (you should) then this is the solution that worked for me:

如果您正在使用 AppCompatActivity(您应该使用),那么这是对我有用的解决方案:

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

You can add this in onCreate(). The support fragment's getActivity() returns a FragmentActivity, and this does not contain the getSupportActionBar() method. Using just getActionBar() gives null-pointer exception if you have AppCompatActivity.

您可以在 onCreate() 中添加它。支持片段的 getActivity() 返回一个 FragmentActivity,它不包含 getSupportActionBar() 方法。如果您有 AppCompatActivity,只使用 getActionBar() 会产生空指针异常。

回答by michal.luszczuk

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

getActionBar().hide()getSupportActionBar().hide()(如果使用 ActionBarCompat v7 lib)。问候

回答by engico

Hide actionBar using this in the required fragment.

在所需的片段中使用它隐藏 actionBar。

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

And Show actionBar with this in your next fragment.

并在下一个片段中使用此显示 actionBar。

getActivity().getActionBar().show();

回答by Magus 14

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

放在保存片段的 ActivitygetSupportActionBar().hide()之前setContentView

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 lagsin hiding action bar

还要添加:((AppCompatActivity) getActivity()).getSupportActionBar().hide()在膨胀布局之前的片段中。如果您正在使用ActionBarActivity它,这会起作用。它还消除了我隐藏操作栏的滞后

回答by Arish Khan

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

您可以简单地将其放入您的 Fragment 类 createView 方法中:-

    View layout = inflater.inflate(R.layout.player_fragment, container, false);
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

回答by Justin Muller

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

您是否getActivity().getSupportActionBar().hide()onCreate()希望 ActionBar 隐藏的片段中尝试过?

I am assuming you are not using ActionBarSherlock.

我假设您没有使用ActionBarSherlock.

回答by griffins

Put this code in fragment in which you want to hide toolbar...

将此代码放在要隐藏工具栏的片段中...

 @Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

回答by macros013

As it was already mentioned, actionbar may be hidden by (requireActivity() as AppCompatActivity).supportActionBar?.hide()call. If you want to show it in some of your fragments and to hide it in some other fragments, it may be convenient to apply default (for your case) visibility in onViewCreatedof your Base fragment:

正如已经提到的,操作栏可能会被(requireActivity() as AppCompatActivity).supportActionBar?.hide()调用隐藏。如果您想在某些片段中显示它并将其隐藏在其他一些片段中,则在onViewCreatedBase 片段中应用默认(针对您的情况)可见性可能会很方便:

abstract class BaseFragment : Fragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.show()
    }
}

and to hide it in particular fragments:

并将其隐藏在特定片段中:

class HiddenActionBarFragment : BaseFragment() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.hide()
    }
}

This solution is more flexible than using onStart- onStopfor visibility change because during the transition onStartof a different fragment will be called earlier than onStopof the current fragment. So the next fragment won't be able to 'override' actionar visibility applied in onStopof the current fragment.

此解决方案比使用更灵活onStart-onStop用于可见性更改,因为在转换期间,onStart不同片段将比onStop当前片段更早地被调用。因此,下一个片段将无法“覆盖”应用于onStop当前片段的动作可见性。

回答by Bad Loser

This solution is for complex non-AppCompat applications that use native ToolBarwhen running Lollipop onwards and native ActionBarotherwise.

此解决方案适用于复杂的非 AppCompat 应用程序,这些应用程序在运行 Lollipop 时使用本机ToolBar,否则使用本机ActionBar

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

它假设您希望在 Fragments 可见时隐藏 ActionBar。

Inside onCreate()in each of your Activities:

在您的每个活动中的onCreate()内部:

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

或(更好)在实现 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 changesto your Fragments or Activities!

使用onActivityCreated()是一种无需更改片段或活动的解决方案!