Add icon with title in CollapsingToolbarLayout

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

I am using CoordinatorLayout to get this effect :

Here is the layout code:

             


        
相关标签:
4条回答
  • 2020-12-08 03:38

    I suggest you to try Childs and dependencies

          public boolean onDependentViewChanged(
    
          CoordinatorLayout parent, 
          CircleImageView avatar, 
          View dependency) {
    
    
          modifyAvatarDependingDependencyState(avatar, dependency);
       }
    
       private void modifyAvatarDependingDependencyState(
        CircleImageView avatar, View dependency) {
            //  avatar.setY(dependency.getY());
            //  avatar.setBlahBlat(dependency.blah / blah);
        } 
    

    http://www.devexchanges.info/2016/03/android-tip-custom-coordinatorlayout.html

    CollapsingToolbarLayout with a custom view

    0 讨论(0)
  • 2020-12-08 03:50

    May be this solve your problem :

    You can position the expanded title wherever you want by using these CollapsingToolbarLayout attributes:

    app:expandedTitleGravity        default is bottom|left -or- bottom|start
    app:expandedTitleMargin
    app:expandedTitleMarginBottom
    app:expandedTitleMarginStart
    app:expandedTitleMarginEnd
    

    Code for layout File :

    <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapse_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                android:fitsSystemWindows="true">
    
                <ImageView
                    android:id="@+id/bgheader"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:fitsSystemWindows="true"
                    android:background="@drawable/sunflowerpic"
                    app:layout_collapseMode="pin" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/MyToolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="parallax" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    

    Then in your java file SetTitle:

     CollapsingToolbarLayout collapsingToolbar =
                    (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
            collapsingToolbar.setTitle("Title");
    

    Add icon to Top corner : use app:layout_collapseMode="pin" with ImagView. For e.g.

    <ImageView
                android:id="@+id/someImage"
                android:layout_width="56dp"
                android:layout_height="wrap_content"
                android:src="@drawable/someDrawable"
                android:padding="16dp"
                android:layout_gravity="top|end"
                app:layout_collapseMode="pin"
                />
    

    Reference to this link Collapsing Toolbar Example

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 03:56

    You can take reference from this example:-

    android ParallaxHeaderViewPager

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