繁体   English   中英

使用带有导航组件的导航抽屉时如何更改操作栏图标(汉堡图标)

[英]how to change Action bar icon (hamburger icon) when using navigation drawer with navigation component

我想更改主页指示器,但在使用导航组件时它不起作用

我从这里尝试了解决方案,但在使用带有喷气背包导航组件的导航抽屉时,没有工作如何更改工具栏图标(汉堡图标)

我的代码

    setSupportActionBar(binding.toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.myTeamFragment,
            R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment)
            .setOpenableLayout(binding.getRoot())
            .build();
    NavigationUI.setupWithNavController(binding.toolbar, getNavController(), appBarConfiguration);

然后作为解决方案提到

ActionBar actionBar = getSupportActionBar();
    getNavController().addOnDestinationChangedListener((controller, destination, arguments) -> {
        if (destination.getId() == R.id.dest0
                || destination.getId() == R.id.dest1
                || destination.getId() == R.id.dest2
                || destination.getId() == R.id.dest3
                || destination.getId() == R.id.dest4){

            actionBar.setHomeAsUpIndicator(R.drawable.ic_hambergur_menu);
        } else {
            actionBar.setHomeAsUpIndicator(R.drawable.ic_hambergure_back);
        }
    });

您正在使用带有Toolbar的设置

NavigationUI.setupWithNavController(binding.toolbar, getNavController(), appBarConfiguration);

然后使用 Toolbar API 代替 ActionBar API:

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.nav_home){
            toolbar.setNavigationIcon(R.drawable.xxxx)
        }
    }

如果您只想显示后退箭头按钮而不是汉堡按钮,则可以从appBarConfiguration中删除要显示后退按钮的片段

In your snippet, the back button won't show up in these fragemnts ( R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment ), remove any of them to show the back button反而。

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.myTeamFragment,
        R.id.myTasksFragment, R.id.meetingsFragment, R.id.freeTimeFragment, R.id.dashboardFilterFragment)
        .setOpenableLayout(binding.getRoot())
        .build();

我不知道,为什么? 但对我来说,它适用于postDelay function。 即使我设置延迟为零

 navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.fragment_orders) {
            view.postDelayed(
                {
                    supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_amount)
                },
                0
            )
        }
    }

添加延迟为 0 mili 的处理程序对我有用。

  @Override
    public void onDestinationChanged(@NonNull NavController controller,
                                     @NonNull NavDestination destination,
                                     @Nullable Bundle arguments) {
        Log.d(TAG, "onDestinationChanged: current destination: "+navController.getCurrentDestination().getLabel());
        this.currentFragmentId = destination.getId();
        if(currentFragmentId == R.id.addItemFragment){
            new Handler().postDelayed(()->{
                mToolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
            },0);
        }
        super.setTitle(destination.getLabel());
    }

暂无
暂无

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

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