Android导航从第一个片段中删除工具栏

问题描述 投票:0回答:1

我正在尝试从Google实施新的Jetpack Navigation框架,但是遇到了问题。我想将我的第一个片段用作登录页面,并且不想在其中包含工具栏。如何从其中一个片段中删除工具栏,然后将其添加到后续片段中?

编辑:尝试查看AppBarConfiguration,但这似乎只影响是否显示后退箭头

android android-toolbar android-jetpack android-navigation
1个回答
1
投票

最终弄清楚如何做。根据android文档,您必须将一个OnDestinationChangedListener添加到导航控制器,然后才能在需要更改其中的常量UI元素的所有不同目标上进行切换。

navController = Navigation.findNavController(this, R.id.nav_host_fragment);
final Toolbar toolbar = findViewById(R.id.toolbar);

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
    @Override
    public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        int id = destination.getId();
        switch (id) {
            case R.id.mainFragment:
                toolbar.setVisibility(View.GONE);
                break;
            default:
                toolbar.setVisibility(View.VISIBLE);
                break;
        }
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.