CollapsingToolbarLayout學習筆記


CollapsingToolbarLayout包裹 Toolbar 的時候提供一個可折疊的 Toolbar,一般作為AppbarLayout的子視圖使用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="160dp">

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

            >

            <ImageView
                android:id="@+id/image"
                app:layout_collapseParallaxMultiplier="0.6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/image"
                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"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>


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

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

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

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        app:backgroundTintMode="multiply"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|end|right"></android.support.design.widget.FloatingActionButton>

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

當未折疊時顯示的是大的Title,可以通過CollapsingToolbarLayout的setTitle()來設置,並通過setExpandedTitleColor來設置其顏色

折疊時顯示的小Title通過setCollapsedTitleTextColor來設置

layout_collapseParallaxMultiplier:CollapsingToolbarLayout滑動時,子視圖的視覺差(然而並沒有覺得有什么區別……OTZ)

CollapseMode:子視圖的折疊模式,有兩種“pin”:固定模式,在折疊的時候最后固定在頂端;“parallax”:視差模式,在折疊的時候會有個視差折疊的效果。我們可以在布局中使用屬性app:layout_collapseMode=”parallax”來改變。(然而也沒有覺得有什么區別……OTZ)

 

注意:

  • Toolbar 的高度layout_height必須明確指定,不能 “wrap_content”,否則Toolbar不會滑動,也沒有折疊效果。
  • 為了能讓FloatingActionButton也能折疊且消失出現,我們必須給FAB設置錨點屬性 

    app:layout_anchor="@id/appbar"

    且設置當前錨點的位置

    app:layout_anchorGravity=”bottom|end|right”

    兩個屬性共同作用才是的FAB 浮動按鈕也能折疊消失,出現。

  • 給需要有折疊效果的組件設置 layout_collapseMode屬性。

 

 

參考文章:

http://blog.csdn.net/feiduclear_up/article/details/46514791


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM