Android進階之光》Design Support Library常用控件(三):AppBarLayout、CollapsingToolbarLayout

AppBarLayout

用AppBarLayout 可讓你定製當某個可滾動View的滾動手勢發生變化時,其內部的子View實現何種動作。

CollapsingToolbarLayout

CollapsingToolbarLayout是用來對Toolbar進行再次包裝的ViewGroup,主要是用於實現摺疊(其實就是看起來像伸縮~)的AppBar效果。它需要放在AppBarLayout佈局裏面,並且作爲AppBarLayout的直接子View。

詳細可參考這裏:玩轉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">

    <!--用AppBarLayout 可讓你定製當某個可滾動View的滾動手勢發生變化時,其內部的子View實現何種動作-->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--AppBarLayout的直接子view,使用layout_scrollFlags設置滾動方式-->
        <!--CollapsingToolbarLayout是用來對Toolbar進行再次包裝的ViewGroup,主要是用於實現摺疊(其實就是看起來像伸縮~)的App Bar效果。它需要放在AppBarLayout佈局裏面,並且作爲AppBarLayout的直接子View。 -->
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="this is title!"
            app:titleEnabled="true">

            <!--視差滾動子View(Parallax scrolling children):子View可以選擇在當前的佈局當時是否以“視差”的方式來跟隨滾動。(PS:其實就是讓這個View的滾動的速度比其他正常滾動的View速度稍微慢一點)。將佈局參數app:layout_collapseMode設爲parallax-->
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/blue"
                app:layout_collapseMode="parallax" />

            <!--引入Toolbar-->
            <!--將子View位置固定(Pinned position children):子View可以選擇是否在全局空間上固定位置,這對於Toolbar來說非常有用,因爲當佈局在移動時,可以將Toolbar固定位置而不受移動的影響。 將app:layout_collapseMode設爲pin-->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:logo="@mipmap/ic_launcher"
                app:navigationIcon="@mipmap/icon_item_detail_back"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <!--NestedScrollView就是可滾動的view-->
    <!--AppBarLayout與NestedScrollView關聯:layout_behavior="@string/appbar_scrolling_view_behavior"-->
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_snack_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="show snack bar" />

            <Button
                android:id="@+id/btn_test_behavior"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="btn_test_behavior" />


        </LinearLayout>

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

    <!--FloatingActionButton-->
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        android:background="@color/colorAccent"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:elevation="15dp"
        android:src="@mipmap/dog"
        app:pressedTranslationZ="10dp" /><!--pressedTranslationZ點擊時陰影的大小-->
</android.support.design.widget.CoordinatorLayout>

代碼裏面有註釋說明。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章