How to change toolbar icons color on collapsing

前端 未结 1 1712
说谎
说谎 2021-02-14 13:25

I\'m using CoordinatorLayout in my project . It works fine but there is a problem I need to fix .I\'m using white actionbar icons and When the CoordinatorLayout is expanded and

相关标签:
1条回答
  • 2021-02-14 13:46

    You can do this similar to as shown in this Stack Overflow post. Thanks to tachyonflux

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    AppBarLayout appBar = (AppBarLayout)  findViewById(R.id.appbar);
    
    appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if ((collapsingToolbar.getHeight() + verticalOffset) < (2 * ViewCompat.getMinimumHeight(collapsingToolbar))) {
                    toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
                } else {
                    toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP);
                }
            }
        });
    

    Similarly you can apply it for any drawable or images within toolbar or collapsing toolbar layout.

    0 讨论(0)
提交回复
热议问题