CollapsingToolbarLayout 介紹和簡單使用
上次我們介紹了AppBarLayout,這一次我們介紹CollapsingToolbarLayout。
CollapsingToolbarLayout 介紹
顧名思義,這是一個可摺疊的Toolbar;不過它的使用必須在AppBarLayout的基礎之上,它必須作為AppBarLayout的直接子類元素使用;否則起不到應用的效果。
在Android Studio 裡建立module/Activity時,就會提供一個ScrollActivity
的模板,這個模板就使用了AppBarLayout 和 CollapsingToolbarLayout 實現了可摺疊的Toolbar。這裡就不在具體介紹如何去建立這個模板了。我們先看一下這個模板實現的大概效果:
xml的設定如下:
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="標題"
/>
</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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
首先我們看一下AppBarLayout ,它設定了app:layout_scrollFlags=”scroll|exitUntilCollapsed”,關於’exitUntilCollapsed’,會涉及到minHeight;當發生向上滾動事件時,AppLayout向上滾動,直到我們設定的minHeight,然後我們的滑動View才開始滾動。就算我們滑動的view完全上滑完畢,我們的AppBarLayout也會一直保留我們設定的民Height顯示在螢幕的上方。但是我們並沒有去設定minHeight呀?從表面上看確實是這樣的。我們去看一下CollapsingToolbarLayout的原始碼來一探究竟。在onLayout方法裡我們可以看到這麼一段程式碼:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// ……省略若干程式碼
// Finally, set our minimum height to enable proper AppBarLayout collapsing
if (mToolbar != null) {
if (mCollapsingTitleEnabled && TextUtils.isEmpty(mCollapsingTextHelper.getText())) {
// If we do not currently have a title, try and grab it from the Toolbar
mCollapsingTextHelper.setText(mToolbar.getTitle());
}
if (mToolbarDirectChild == null || mToolbarDirectChild == this) {
setMinimumHeight(getHeightWithMargins(mToolbar));
mToolbarDrawIndex = indexOfChild(mToolbar);
} else {
setMinimumHeight(getHeightWithMargins(mToolbarDirectChild));
mToolbarDrawIndex = indexOfChild(mToolbarDirectChild);
}
} else {
mToolbarDrawIndex = -1;
}
updateScrimVisibility();
}
在上面程式碼裡我們可以看到它Toolbar的高度作為minHeight了,當我們上滑時,滑到只剩下Toolbar高度時,CollapsingToolbarLayout 就不會再摺疊了,會一直保持在螢幕的頂端。
app:layout_collapseMode
除此之外,我們還可以發下在Toolbar裡還設定了app:layout_collapseMode="pin"
,這裡涉及到了摺疊模式。我們依次瞭解一下.
- none:這個是預設屬性,Toolbar會正常顯示,不會有摺疊行為;
- pin : Toolbar 摺疊以後會放到螢幕頂端,固定不動;
- parallax:意為視差,當我們上滑時會有視差效果,但是我們之前設定在Toolbar上的東西會滑出螢幕。
具體我們示例:
當我們把摺疊模式設定為 pin
時:
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorAccent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:navigationIcon="@mipmap/ic_launcher"
/>
</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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
為了看Toolbar的效果,我在Toolbar上加了一個navigationIcon;效果如下:
這裡ToolBar的標題會有摺疊效果,但是Toolbar會一直在螢幕的頂端不動。
接下來我們看 parallax
:
上程式碼:
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorAccent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="parallax"
app:title="標題"
app:navigationIcon="@mipmap/ic_launcher"
/>
</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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
這裡我們只改了一個app:layout_collapseMode
,我們看一下效果:
這裡只有摺疊的效果,但是Toolbar會滑出螢幕。至於還有 none
,就不在介紹了,沒有任何效果。
app:contentScrim
不知道有有沒有細心同學看到這個app:contentScrim
屬性。這個是用來設定摺疊時Toolbar所在位置的顏色,並且在滑動的時候會有一定的過渡。
剛開始研究的時候,這個屬性老是不起作用,後來發現,當CollapsingToolbarLayout裡只有的一個Toolbar時,會使用Theme指定的值。當我們在CollapsingToolbarLayout裡新增其它的控制元件,並且這個控制元件必須是在Toolbar的上方時,這個屬性才會起作用。 接下來我們看一下這個屬性的效果。
先上程式碼:
<?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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorAccent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:background="@drawable/header_bar"
android:layout_width="match_parent"
android:layout_height="260dp"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="標題"
app:navigationIcon="@mipmap/ic_launcher"
/>
</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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
在Toolbar上面加了一個ImageView,然後在CollapsingToolbarLayout上設定了app:contentScrim=”?attr/colorAccent”,是一個粉紅色。我們看效果:
關於CollapsingToolbarLayout的基本的幾個點就差不多完了,我們需要注意的是:
- app:layout_scrollFlags 要放到AppBarLayout的直接子控制元件上
- app:contentScrim 要放到CollapsingToolbarLayout上
- app:contentScrim 這個屬性,需要我們在CollapsingToolbarLayout裡新增其它的控制元件,並且這個控制元件必須是在Toolbar的上方時,這個屬性才會起作用