Material Design - CollapsingToolbarLayout

關(guān)鍵字:CollapsingToolbarLayout、材料設(shè)計(jì)
項(xiàng)目地址:AboutMaterialDesign


安卓材料設(shè)計(jì)中,這是最后一個(gè)用于布局中的類(lèi) -- CollapsingToolbarLayout。CollapsingToolbarLayout 是繼承自 FramenLayout 的子類(lèi),它為我們提供了滑動(dòng)漸變的效果。除此之外,并沒(méi)有其他功能,因此使用的場(chǎng)景其實(shí)非常單一,而且明白了主要的參數(shù),就可以駕馭它所提供的功能。。我們先看看基本效果:

GIF.gif

GIF.gif

下面先說(shuō)注意點(diǎn):

  • 1.建議使用最新的 design 包,因?yàn)榘姹驹诓粩喔?,越新的版?bug 越少。比如這次測(cè)試的時(shí)候,我在三星手機(jī)上發(fā)現(xiàn) Toolbar 的展示位置不對(duì),后來(lái)更新了 design 包的版本,問(wèn)題就沒(méi)了。
  • 2.通常使用 CollapsingToolbarLayout 時(shí),結(jié)構(gòu)都是固定的,即: CoordinatorLayout - AppBarLayout - CollapsingToolbarLayout - ToolbarLayout && ImageView。 且 Toolbar 外部的容器和 ImageView 都需要有 fitSystemWindow="true" 屬性才能達(dá)到上圖效果
  • 3.Toolbar的高度必須固定,不能設(shè)置為"wrap_content",否則Toolbar不會(huì)滑動(dòng),也沒(méi)有折疊效果
  • 4.CoordinatorLayout的直接子View必須是一個(gè)可滑動(dòng)的控件,并且內(nèi)部有內(nèi)容可以滑動(dòng)。同時(shí)需要設(shè)置app:layout_behavior
  • 5.當(dāng)效果出現(xiàn)重疊或者其他不如意的效果,那一般都和你在用的 CollapsingToolbarLayout 沒(méi)有關(guān)系,仔細(xì)檢查一下 Toolbar 和 AppBarLayout 的屬性設(shè)置對(duì)不對(duì),因?yàn)?CollapsingToolbarLayout 可以設(shè)置的東西真的不多。

一、我就是列一下屬性

下面介紹一下 CollapsingToolbarLayout 的屬性:

contentScrim                        -- 折疊后內(nèi)容的顯示顏色
statusBarScrim                      -- 折疊后菜單的顯示顏色
expandedTitleGravity                -- 展開(kāi)時(shí) Toolbar title 的顯示位置
collapsedTitleGravity               -- 折疊時(shí) Toolbar title 的顯示位置
expandedTitleMargin                 -- 對(duì)應(yīng)的有四個(gè) 上下左右
expandedTitleTextAppearance         -- 展開(kāi) title 樣式
collapsedTitleTextAppearance        -- 折疊 title 樣式
scrimVisibleHeightTrigger           -- 設(shè)置收起多少高度時(shí),顯示ContentScrim的內(nèi)容
layout_collapseMode                 -- 折疊模式
                                        -none 跟隨滾動(dòng)的手勢(shì)進(jìn)行折疊。
                                        -parallax 視差滾動(dòng),搭配layout_collapseParallaxMultiplier(視差因子)使用。
                                        -pin 固定不動(dòng)。
layout_collapseParallaxMultiplier   -- 視差因子,范圍:0-1,默認(rèn)0.5

二、我就是貼一下代碼

附上開(kāi)頭第二張動(dòng)態(tài)圖的布局代碼:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:fitsSystemWindows="true" ----------------------------------------------- ***
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:fitsSystemWindows="true" ------------------------------------------- ***
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:fitsSystemWindows="true" --------------------------------------- ***
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/detail_backdrop_height"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" --------------------- ***
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/backdrop"
                android:fitsSystemWindows="true" ------------------------------------ ***
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" /> ------------------------------- ***

            <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>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_down"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

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

參考:
1.CheeseDemo
2.StackOverFlow

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

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,308評(píng)論 25 708
  • 前言 本文同步發(fā)布到我的掘金專欄Material Design之 AppbarLayout 開(kāi)發(fā)實(shí)踐總結(jié) 前一篇文...
    依然范特稀西閱讀 28,221評(píng)論 37 144
  • 奶奶家的樓梯上 兒子:我想買(mǎi)個(gè)面包吃 我:你想買(mǎi)個(gè)面包吃 兒子:恩 兒子開(kāi)始加快爬樓梯的步伐 我的內(nèi)心:天那,真的...
    桃夭和其華閱讀 364評(píng)論 2 0
  • 喜歡上水彩了,并且入迷了。有時(shí)間就想畫(huà)畫(huà)。最近很喜歡畫(huà)花,從開(kāi)始接觸水彩到現(xiàn)在畫(huà)了有一百來(lái)張了。去公園拍照感覺(jué)自己...
    紫絮飄然lei66閱讀 204評(píng)論 0 0
  • 正如兩年前一樣,我能感受到她的存在,一直都能。 如今才知道,當(dāng)初的我不是幼稚,也不是瘋癲。 我能感受到,她活在我心...

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