Material Design ( 5 ) - CollapsingToolbarLayout

首發(fā)于我的博客,轉(zhuǎn)載請注明作者和原文鏈接赖歌。

CollapsingToolbarLayout通常用來在布局中包裹一個Toolbar养涮,同時作為AppBarLayout的直接子View芯肤,才可以實現(xiàn)“折疊效果”的標(biāo)題欄逞壁。

CollapsingToolbarLayout屬性說明

  • app:title / setTitle()
    ToolBar的標(biāo)題毛俏,當(dāng)CollapsingToolbarLayout展開時考余,title顯示的是大字體砂豌,在折疊的過程中,title不斷變小到一定大小的效果光督。

  • app:contentScrim / setContentScrim()
    ToolBar被折疊到頂部固定時候的背景阳距。

  • app:statusBarScrim / setStatusBarScrim()
    折疊后狀態(tài)欄的背景。

  • app:scrimVisibleHeightTrigger
    設(shè)置收起多少高度時结借,顯示ContentScrim的內(nèi)容筐摘。

  • app:scrimAnimationDuration
    展開狀態(tài)和折疊狀態(tài)之間,內(nèi)容轉(zhuǎn)換的動畫時間船老。

下面兩個屬性是作為CollapsingToolbarLayout的子View才能設(shè)置并生效的:

  • app:layout_collapseMode折疊模式
    none 跟隨滾動的手勢進(jìn)行折疊咖熟。
    parallax 視差滾動,搭配layout_collapseParallaxMultiplier(視差因子)使用柳畔。
    pin 固定不動馍管。

  • app:layout_collapseParallaxMultiplier
    視差因子,范圍:0-1薪韩,默認(rèn)0.5确沸。

layout布局

<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"
    android:fitsSystemWindows="true"
    tools:context=".collapsingtoolbarlayout.CollapsingToolbarLayoutActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:id="@+id/app_bar_layout"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/collapsing_toolbar_layout"
            app:statusBarScrim="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="CollapsingToolbarLayout">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"
                android:scaleType="centerCrop"
                android:src="@drawable/toolbar"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_collapseMode="pin"
                android:text="pin:固定不動"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:text="pin:固定不動"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                app:layout_collapseMode="parallax"
                android:text="parallax:\n視差滾動默認(rèn)0.5"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:layout_collapseParallaxMultiplier="0.25"
                app:layout_collapseMode="parallax"
                android:text="parallax:\n視差滾動0.25"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|right"
                app:layout_collapseParallaxMultiplier="0.75"
                app:layout_collapseMode="parallax"
                android:text="parallax:\n視差滾動0.75"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_collapseMode="none"
                android:text="none:跟隨滾動"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top|right"
                android:text="不設(shè)置:跟隨滾動"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:navigationIcon="?homeAsUpIndicator"
                app:layout_collapseMode="pin"
                app:title="Toolbar">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="真正的Toolbar區(qū)域"/>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AppBarLayout底部固定布局"
            android:padding="16dp"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/ipsum"/>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

注意

  • 最頂層的布局必須是CoordinatorLayout。
  • CoordinatorLayout的直接子View必須是一個可滑動的控件俘陷,并且內(nèi)部有內(nèi)容可以滑動罗捎。同時需要設(shè)置app:layout_behavior
  • Toolbar的高度必須固定拉盾,不能設(shè)置為"wrap_content"宛逗,否則Toolbar不會滑動,也沒有折疊效果盾剩。
  • 需要注意的是這里幾次設(shè)置android:fitsSystemWindows="true",分別在CoordinatorLayout替蔬、AppBarLayout告私、ImageView。這樣之后達(dá)到的效果就是這張圖片的內(nèi)容可以延伸到狀態(tài)欄承桥。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驻粟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子凶异,更是在濱河造成了極大的恐慌蜀撑,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剩彬,死亡現(xiàn)場離奇詭異酷麦,居然都是意外死亡,警方通過查閱死者的電腦和手機喉恋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門沃饶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來母廷,“玉大人,你說我怎么就攤上這事糊肤∏倮ィ” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵馆揉,是天一觀的道長业舍。 經(jīng)常有香客問我,道長升酣,這世上最難降的妖魔是什么舷暮? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮拗踢,結(jié)果婚禮上脚牍,老公的妹妹穿的比我還像新娘。我一直安慰自己巢墅,他們只是感情好诸狭,可當(dāng)我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著君纫,像睡著了一般驯遇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蓄髓,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天叉庐,我揣著相機與錄音,去河邊找鬼会喝。 笑死陡叠,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肢执。 我是一名探鬼主播枉阵,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼预茄!你這毒婦竟也來了兴溜?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤耻陕,失蹤者是張志新(化名)和其女友劉穎拙徽,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诗宣,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡膘怕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了召庞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淳蔼。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡侧蘸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鹉梨,到底是詐尸還是另有隱情讳癌,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布存皂,位于F島的核電站晌坤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旦袋。R本人自食惡果不足惜骤菠,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疤孕。 院中可真熱鬧商乎,春花似錦、人聲如沸祭阀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽专控。三九已至抹凳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間伦腐,已是汗流浹背赢底。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留柏蘑,地道東北人蛔屹。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓慨默,卻偏偏與公主長得像痘绎,于是被迫代替她去往敵國和親姑裂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容