Material Design之CollapsingToolbarLayout使用(折疊式標題欄)

CollapsingToolbarLayout作用是提供了一個可以折疊的Toolbar,它繼承至FrameLayout,給它設置layout_scrollFlags,它可以控制包含在CollapsingToolbarLayout中的控件(如:ImageView、Toolbar)在響應layout_behavior事件時作出相應的scrollFlags滾動事件(移除屏幕或固定在屏幕頂端)。建議在Android5.0+使用比較好。低版本不太適配

demo

使用CollapsingToolbarLayout:

<android.support.design.widget.AppBarLayout  
        android:layout_width="match_parent"  
        android:layout_height="256dp"  
        android:fitsSystemWindows="true">  
        <android.support.design.widget.CollapsingToolbarLayout  
            android:id="@+id/collapsing_toolbar_layout"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            app:contentScrim="#30469b"  
            app:expandedTitleMarginStart="48dp"  
            app:layout_scrollFlags="scroll|exitUntilCollapsed">  
  
            <ImageView  
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:scaleType="centerCrop"  
                android:src="@mipmap/bg"  
                app:layout_collapseMode="parallax"  
                app:layout_collapseParallaxMultiplier="0.7"  />  
  
            <android.support.v7.widget.Toolbar  
                android:id="@+id/toolbar"  
                android:layout_width="match_parent"  
                android:layout_height="?attr/actionBarSize"  
                app:layout_collapseMode="pin" />  
        </android.support.design.widget.CollapsingToolbarLayout>  
    </android.support.design.widget.AppBarLayout>  

我們在CollapsingToolbarLayout中設置了一個ImageView和一個Toolbar。并把這個CollapsingToolbarLayout放到AppBarLayout中作為一個整體。

1、在CollapsingToolbarLayout中:

我們設置了layout_scrollFlags:關于它的值我這里再說一下:

  • scroll - 想滾動就必須設置這個。
  • enterAlways - 實現(xiàn)quick return效果, 當向下移動時,立即顯示View(比如Toolbar)。
  • exitUntilCollapsed - 向上滾動時收縮View,但可以固定Toolbar一直在上面。
  • enterAlwaysCollapsed - 當你的View已經(jīng)設置minHeight屬性又使用此標志時,你的View只能以最小高度進入,只有當滾動視圖到達頂部時才擴大到完整高度。

其中還設置了一些屬性,簡要說明一下:

  • contentScrim - 設置當完全CollapsingToolbarLayout折疊(收縮)后的背景顏色。(收縮前,是ToolBar的背景色,透明即可)
  • expandedTitleMarginStart - 設置擴張時候(還沒有收縮時)title向左填充的距離。
  • title:當titleEnable設置為true的時候,在toolbar展開的時候,顯示大標題,toolbar收縮時,顯示為toolbar上面的小標題。
  • scrimAnimationDuration:該屬性控制toolbar收縮時,顏色變化的動畫持續(xù)時間。即顏色變?yōu)閏ontentScrim所指定的顏色進行的動畫所需要的時間。
  • expandedTitleGravity:指定toolbar展開時,title所在的位置。類似的還有expandedTitleMargin、collapsedTitleGravity這些屬性。
  • collapsedTitleTextAppearance:指定toolbar收縮時,標題字體的樣式,類似的還有expandedTitleTextAppearance。

2、在ImageView控件中:

我們設置了:
layout_collapseMode (折疊模式) - 有兩個值:

  • pin - 設置為這個模式時,當CollapsingToolbarLayout完全收縮后,Toolbar還可以保留在屏幕上。
  • parallax - 設置為這個模式時,在內(nèi)容滾動時,CollapsingToolbarLayout中的View(比如ImageView)也可以同時滾動,實現(xiàn)視差滾動效果,通常和layout_collapseParallaxMultiplier(設置視差因子)搭配使用。
  • layout_collapseParallaxMultiplier(視差因子) - 設置視差滾動因子,值為:0~1。

3、在Toolbar控件中:

我們設置了layout_collapseMode(折疊模式):為pin。

綜上分析:當設置了layout_behavior的控件響應起了CollapsingToolbarLayout中的layout_scrollFlags事件時,ImageView會有視差效果的向上滾動移除屏幕,當開始折疊時CollapsingToolbarLayout的背景色(也就是Toolbar的背景色)就會變?yōu)槲覀冊O置好的背景色,Toolbar也一直會固定在最頂端。


【注】:使用CollapsingToolbarLayout時必須把title設置到CollapsingToolbarLayout上,設置到Toolbar上不會顯示。即:

mCollapsingToolbarLayout.setTitle(" ");

該變title的字體顏色:

  • 擴張時候的title顏色:
    mCollapsingToolbarLayout.setExpandedTitleColor();
  • 收縮后在Toolbar上顯示時的title的顏色:
    mCollapsingToolbarLayout.setCollapsedTitleTextColor();
    這個顏色的過度變化其實CollapsingToolbarLayout已經(jīng)幫我們做好,它會自動的過度,比如我們把收縮后的title顏色設為綠色

布局文件:

**[html]** [view plain](http://blog.csdn.net/u010687392/article/details/46906657#) [copy](http://blog.csdn.net/u010687392/article/details/46906657#)
 [print](http://blog.csdn.net/u010687392/article/details/46906657#)[?](http://blog.csdn.net/u010687392/article/details/46906657#)

<android.support.design.widget.CoordinatorLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity">  
  
    <android.support.design.widget.AppBarLayout  
        android:layout_width="match_parent"  
        android:layout_height="256dp"  
        android:fitsSystemWindows="true">  
        <android.support.design.widget.CollapsingToolbarLayout  
            android:id="@+id/collapsing_toolbar_layout"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            app:contentScrim="#30469b"  
            app:expandedTitleMarginStart="48dp"  
            app:layout_scrollFlags="scroll|exitUntilCollapsed">  
  
            <ImageView  
                android:layout_width="match_parent"  
                android:layout_height="match_parent"  
                android:scaleType="centerCrop"  
                android:src="@mipmap/bg"  
                app:layout_collapseMode="parallax"  
                app:layout_collapseParallaxMultiplier="0.7"  />  
  
            <android.support.v7.widget.Toolbar  
                android:id="@+id/toolbar"  
                android:layout_width="match_parent"  
                android:layout_height="?attr/actionBarSize"  
                app:layout_collapseMode="pin" />  
        </android.support.design.widget.CollapsingToolbarLayout>  
    </android.support.design.widget.AppBarLayout>  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical"  
        app:layout_behavior="@string/appbar_scrolling_view_behavior">  
        <android.support.v7.widget.RecyclerView  
            android:id="@+id/recyclerView"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:scrollbars="none" />  
    </LinearLayout>  
</android.support.design.widget.CoordinatorLayout>  

Java:

 //設置狀態(tài)欄
        //沉浸式狀態(tài)欄
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0之上
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
 
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);  
    setSupportActionBar(mToolbar);  
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View v) {  
            onBackPressed();  
        }  
    });  
    //使用CollapsingToolbarLayout必須把title設置到CollapsingToolbarLayout上,設置到Toolbar上則不會顯示  
    CollapsingToolbarLayout mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);  
    mCollapsingToolbarLayout.setTitle("CollapsingToolbarLayout");  
    //通過CollapsingToolbarLayout修改字體顏色  
    mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//設置還沒收縮時狀態(tài)下字體顏色  
    mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);//設置收縮后Toolbar上字體的顏色

CollapsingToolbarLayout的展開與折疊

使用官方提供的 AppBarLayout.OnOffsetChangedListener就能實現(xiàn)了,不過要封裝一下才好用。

自定義一個繼承了 AppBarLayout.OnOffsetChangedListener的類,這里命名為AppBarStateChangeListener:

public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
 
    public enum State {
        EXPANDED,
        COLLAPSED,
        IDLE
    }
 
    private State mCurrentState = State.IDLE;
 
    @Override
    public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        if (i == 0) {
            if (mCurrentState != State.EXPANDED) {
                onStateChanged(appBarLayout, State.EXPANDED);
            }
            mCurrentState = State.EXPANDED;
        } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }
 
    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}

然后這樣使用它:

        mAppBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if( state == State.EXPANDED ) {
                    
                    //展開狀態(tài)
                    
                }else if(state == State.COLLAPSED){
                    
                    //折疊狀態(tài)
                     
                }else {
                
                    //中間狀態(tài)
                
                }
            }
        });\

Demo地址:https://github.com/huangshuyuan/ToolBarDemo/

參考資料:

Android5.0+(CollapsingToolbarLayout)
Android實現(xiàn)沉浸式通知欄通知欄背景顏色跟隨app導航欄背景顏色而改變
android滑動toolbar 很炫的標題欄
白底黑字!Android淺色狀態(tài)欄黑色字體模式

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容