如何使用 Android 导航组件在某些片段中隐藏操作栏? [英] How to hide actionbar in some fragments with Android Navigation Components?

查看:34
本文介绍了如何使用 Android 导航组件在某些片段中隐藏操作栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android 导航组件来导航片段.我可以通过在主活动中使用此代码轻松设置操作栏:

I am using android navigation components to navigate fragments. 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);

但是如果我想在某些片段中隐藏 supportActionbar 那么最好的方法是什么?

But If I want to hide the supportActionbar in some of the fragments then what should be the best approach?

推荐答案

对于想要隐藏SupportActionBar的片段,可以在onResume()中隐藏用 .hide(),然后在 onStop() 中用 .show()

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

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

这篇关于如何使用 Android 导航组件在某些片段中隐藏操作栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆