简体   繁体   English

如何使用 Android 导航组件在某些片段中隐藏操作栏?

[英]How to hide actionbar in some fragments with Android Navigation Components?

I am using android navigation components to navigate fragments.我正在使用 android 导航组件来导航片段。 I can easily set action bar by using this code in the Main Activity:我可以通过在主活动中使用此代码轻松设置操作栏:

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

But If I want to hide the supportActionbar in some of the fragments then what should be the best approach?但是如果我想在某些片段中隐藏 supportActionbar 那么最好的方法是什么?

For the fragments that you want to hide the SupportActionBar , you can hide it in onResume() with .hide() , and show it again in onStop() with .show()对于要隐藏SupportActionBar的片段,可以使用 .hide() 将其隐藏在onResume() .hide()中,并使用.show()onStop()中再次显示

@Override
public void onResume() {
    super.onResume();
    ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
    if (supportActionBar != null)
        supportActionBar.hide();
}

@Override
public void onStop() {
    super.onStop();
    ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
    if (supportActionBar != null)
        supportActionBar.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM