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

tah*_*qvi 8 java android android-fragments android-navigation android-architecture-navigation

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

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
Run Code Online (Sandbox Code Playgroud)

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

Zai*_*ain 11

对于要隐藏的片段,SupportActionBar可以将其隐藏在onResume()with 中.hide(),然后在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();
}
Run Code Online (Sandbox Code Playgroud)

  • 它正在工作,但是当您到达想要隐藏操作栏的片段时,它会显示操作栏一段时间并使用默认动画隐藏它 (2认同)