在android中折叠时更改工具栏的颜色

时间:2018-03-23 18:54:14

标签: java android toolbar

我有折叠工具栏,我想在完全折叠时更改颜色。我得到的是默认颜色。

这是我的代码

  private void initCollapsingToolbar() {
    final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    // collapsingToolbar.setTitle(" ");
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    // appBarLayout.setExpanded(true);

    // hiding & showing the title when toolbar expanded & collapsed

    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @SuppressLint("ResourceAsColor")
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            int scrollRange = -1;
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            //Check if the view is collapsed
            if (scrollRange == 0) {
                toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
            } else {
               // toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
            }
        }




        //Initialize the size of the scroll


    });
}  

0 个答案:

没有答案