CoordinatorLayout 實現了Material Design中的滾動效果

文章內容主要方便自己查閱.


CoordinatorLayout

  • 佈局截圖
    佈局截圖

  • 佈局結構

-CoordinatorLayout
    -AppBarLayout
        CollapsingToolbarLayout
        Toolbar
    -NestedScrollView
  • 佈局代碼
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.lukuqi.newone.activity.SettingActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorAccent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <RelativeLayout...>

            <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:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <RelativeLayout...>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
  • 佈局標籤
    CollapsingToolbarLayout中app:layout_scrollFlags屬性必須添加scroll這個flag,這樣view才能滾動,否則一直固定在頂部,也可以添加其他的flag有:
    -enterAlways:上滑滾動view可見。
    -enterAlwaysCollapsed:當view滾動到一個高度開始慢慢出現。
    -exitUntilCollapsed:當view滾到到一個最小高度將消失。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章