-
CoordinatorLayout 介紹
1.作為頂層布局
2.調(diào)度協(xié)調(diào)子布局
CoordinatorLayout提供了很棒的交互體驗(yàn)奋救,并且也有良好的定制性榄棵,可以制作自己想要的交互效果纠俭,下面看下一些基本用法。
-
A.很常見的一種效果
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/appbar"
android:layout_width="match_parent"
android:layout_height="220dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_collapseParallaxMultiplier="0.6"
>
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:fitsSystemWindows="true"
android:src="@drawable/nner"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
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">
<include layout="@layout/content_main"/>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="@android:drawable/ic_dialog_email"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
其實(shí)只是xml文件來實(shí)現(xiàn)的岁忘,需要注意有以下幾點(diǎn):
a.
ToolBar的標(biāo)題蠢壹,使用setTitle()方法設(shè)置title。
b.
Content scrim:ToolBar被折疊到頂部固定時(shí)候的背景入宦,
可以使用setContentScrim(Drawable)方法或者在屬性中
使用 app:contentScrim=”?attr/colorPrimary”來改變背景哺徊。
c.
Parallax scrolling children:CollapsingToolbarLayout滑動(dòng)時(shí),
子視圖的視覺差
app:layout_collapseParallaxMultiplier=”0.6”改變。
值de的范圍0.0~1.0乾闰,值越大視差越大落追。
d.
CollapseMode :子視圖的折疊模式,在子視圖設(shè)置涯肩,有兩種
“pin”:固定模式轿钠,在折疊的時(shí)候最后固定在頂端雹熬;
“parallax”:視差模式,在折疊的時(shí)候會(huì)有個(gè)視差折疊的效果谣膳。
我們可以在布局中使用屬性app:layout_collapseMode=”parallax”來改變。
e.
layout_anchor 的屬性铅乡,連同 layout_anchorGravity 一起继谚,設(shè)置懸浮view FloatingActionButton的位置 。
f.
app:layout_scrollFlags="scroll|exitUntilCollapsed"
這個(gè)屬性是所有不停留在頂端的容器或者view都要設(shè)置的阵幸,如果不設(shè)置則會(huì)自向上滑動(dòng)的時(shí)候停留在頂部花履,
不離開屏幕 scrollFlags 有四個(gè)值 常用的是上面兩個(gè),其他兩個(gè)再配合 minHeight屬性使用時(shí)挚赊,有不同的動(dòng)畫效果.
layout_scrollFlags的四個(gè)值:
1>scroll: 所有想滾動(dòng)出屏幕的view都需要設(shè)置這個(gè)flag诡壁, 沒有設(shè)置這個(gè)flag的view將被固定在屏幕頂部。例如荠割,TabLayout 沒有設(shè)置這個(gè)值妹卿,將會(huì)停留在屏幕頂部。
2>enterAlways: 設(shè)置這個(gè)flag時(shí)蔑鹦,向下的滾動(dòng)都會(huì)導(dǎo)致該view變?yōu)榭梢姸峥耍瑔⒂每焖佟胺祷啬J健薄?/p>
3>enterAlwaysCollapsed: 當(dāng)你的視圖已經(jīng)設(shè)置minHeight屬性又使用此標(biāo)志時(shí),你的視圖只能已最小高度進(jìn)入嚎朽,只有當(dāng)滾動(dòng)視圖到達(dá)頂部時(shí)才擴(kuò)大到完整高度铺纽。
4>exitUntilCollapsed: 滾動(dòng)退出屏幕,最后折疊在頂端哟忍。
-
B.頂部懸浮不再使用gone visible 實(shí)現(xiàn)狡门,使用CoordinatorLayout來控制
xml文件如下:
<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:layout_width="match_parent"
android:layout_height="250dp"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll"
app:contentScrim="#000000"
android:layout_width="match_parent"
android:layout_height="220dp"
android:fitsSystemWindows="true"
>
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:fitsSystemWindows="true"
android:src="@drawable/nner"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
<TextView
android:background="#ffffff"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="懸浮view"/>
</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="121212"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
這樣的效果只需要把想要懸浮在頂部的view
寫在appbarlayout 最外層里面,NestedScrollview外面就行了锅很, so easy,
最常用的是和TabLayout搭配使用其馏。
-
C. 自定義Behavior,實(shí)現(xiàn)非常棒的動(dòng)畫效果粗蔚。
<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="121212"/>
</android.support.v4.widget.NestedScrollView>
app:layout_behavior="@string/appbar_scrolling_view_behavior">
這個(gè)@String其實(shí)是一個(gè)類尝偎,這個(gè)類可以自定義去實(shí)現(xiàn),
以下提供一些講的挺好的文章鹏控,想要深入探究的可以看看致扯。
http://www.open-open.com/lib/view/open1453021356261.html
http://www.reibang.com/p/a506ee4afecb
http://www.reibang.com/p/488283f74e69