CoordinatorLayout(使協調)(一般控制子View用layout_gravity的屬性)
的使用核心是Behavior
,Behavior
就是執行你定製的動作。在講Behavior
之前必須先理解兩個概念:Child
和Dependency
,Child
是CoordinatorLayout
的子View
;其實Child
是指要執行動作的CoordinatorLayout
的子View
。而Dependency
是指Child
依賴的View
就是如過Dependency
這個View發生了變化,那麼Child
這個View
就要相應發生變化。發生變化是具體發生什麼變化呢?這裏就要引入Behavior
,Child
發生變化的具體執行的代碼都是放在Behavior
這個類裏面。(Behaviorz)demo1:
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout//主要是用來包裝toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@drawable/ab"
app:layout_scrollFlags="scroll"//相互協同的屬性 設置爲scroll
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
自己向下滑動等到自己全部顯示時,再把觸摸事件還給nestedScrollView,
它纔會向下滑動,而向上滑動不受影響
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
exitUntilCollapsed"
:值設爲exitUntilCollapsed
的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度後,再響應ScrollView的內部滑動事件。3 CollapsingToolbarLayout
CollapsingToolbarLayout
是用來對Toolbar
進行再次包裝的ViewGroup
,主要是用於實現摺疊(其實就是看起來像伸縮~)的App Bar效果。它需要放在AppBarLayout
佈局裏面,並且作爲AppBarLayout
的直接子View
。CollapsingToolbarLayout
主要包括幾個功能
(1) 摺疊Title(Collapsing title):當佈局內容全部顯示出來時,title是最大的,但是隨着View逐步移出屏幕頂部,title變得越來越小。你可以通過調用setTitle函數來設置title。
(2)內容紗布(Content scrim):根據滾動的位置是否到達一個閥值,來決定是否對View“蓋上紗布”。可以通過setContentScrim(Drawable)來設置紗布的圖片.
(3)狀態欄紗布(Status bar scrim):根據滾動位置是否到達一個閥值決定是否對狀態欄“蓋上紗布”,你可以通過setStatusBarScrim(Drawable)
來設置紗布圖片,但是只能在LOLLIPOP
設備上面有作用。
app:layout_collapseMode
設爲parallax
app:layout_collapseMode
設爲pin
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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
exitUntilCollapsed
:值設爲exitUntilCollapsed
的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度後,再響應ScrollView的內部滑動事件。android:id="@+id/main.backdrop"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@drawable/ab"
app:layout_collapseMode="parallax" />
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView//裏面只能有一個子child
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.design.widget.CoordinatorLayout>