Add icon with title in CollapsingToolbarLayout

后端 未结 4 1634
独厮守ぢ
独厮守ぢ 2020-12-08 03:24

I am using CoordinatorLayout to get this effect :

Here is the layout code:

             


        
4条回答
  •  悲哀的现实
    2020-12-08 03:52

    You may try the following

    Reference for Co-Ordinator Layout

    Now inside your MainActivity.java

    private void handleToolbarTitleVisibility(float percentage) {
        if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
    
            if(!mIsTheTitleVisible) {
                startAlphaAnimation(textviewTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                toolbar.setAlpha(0.9f);
                toolbar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.Primary)));
                mIsTheTitleVisible = true;
            }
        } 
        else {
            if (mIsTheTitleVisible) {
                startAlphaAnimation(textviewTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                toolbar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
                mIsTheTitleVisible = false;
            }
        }
    }
    

    Note: Keep the toolbars background transparent when expanded.

提交回复
热议问题