當(dāng)我在AS上新建一個(gè)module時(shí),系統(tǒng)默認(rèn)的最外層布局不再是我們熟悉的五大布局中的一種,而是一個(gè)全新的布局:CoordinatorLayout。它是Material風(fēng)格的重要組件, 作為布局的頂層控件,協(xié)調(diào)(Coordinate)其他組件, 實(shí)現(xiàn)聯(lián)動(dòng)。
下面來看一個(gè)最簡單的例子,CoordinatorLayout與FloatingActionButton的使用,它可以使浮動(dòng)按鈕上下移動(dòng),為Snackbar流出空間來展示。
定義的布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.lingyun.coordinatorlayoutdemo.MainActivity"> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
代碼就很簡單了,如下:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } 就是通過findViewById獲取FloatingActionButton,設(shè)置點(diǎn)擊事件,在onclick中讓Snackbar顯示一下即可。那么,效果圖就像下面展示的莪一樣:

接下來看一個(gè)高級點(diǎn)的效果,就是標(biāo)題欄,也就是ToolBar的擴(kuò)展與收縮效果。要想要ToolBar響應(yīng)滾動(dòng)事件,這里我們需要用到一個(gè)控件:AppBarLayout,這個(gè)控件必須作為CoordinatorLayout的直接子View,才會響應(yīng)滾動(dòng)事件。首先因?yàn)槲覀兊腡ooBar是需要響應(yīng)滾動(dòng)的視圖,所以需要為其配置一個(gè)屬性:layout_scrollFlags。然后呢,我們需要定義一下AppBarLayout與滾動(dòng)視圖(如RecyclerView,NestedScrollView等可以支持嵌套滾動(dòng)的控件)supportlibrary包含了一個(gè)特殊的字符串資源@string/appbar_scrolling_view_behavior,它和AppBarLayout.ScrollingViewBehavior相匹配,用來通知AppBarLayout 這個(gè)特殊的view何時(shí)發(fā)生了滾動(dòng)事件,這個(gè)behavior需要設(shè)置在觸發(fā)事件(滾動(dòng))的view之上。最終layout布局如下:
主布局(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/appbar_main"/> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
appbar_main.xml布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout 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="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <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" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout>
content_main.xml布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView 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" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="你是誰?你從哪里來?你到哪里去?"/> </android.support.v4.widget.NestedScrollView>
效果圖如下:

通過效果顯示,當(dāng)視圖在滾動(dòng)的時(shí)候,ToolBar滾出了屏幕,為內(nèi)容區(qū)域留出了更大空間。其中控制ToolBar是否可以滾出屏幕的屬性是由app:layout_scrollFlags="scroll|enterAlways"。來說一下這個(gè)屬性,要想滾出屏幕layout_scrollFlags必須設(shè)置scrll這個(gè)flag。剩下的幾個(gè)flag解釋如下:
enterAlways:只要滾動(dòng)視圖向下滾動(dòng),view就會顯示出來。
enterAlwaysCollapsed:顧名思義,這個(gè)flag定義的是何時(shí)進(jìn)入(已經(jīng)消失之后何時(shí)再次顯示)。假設(shè)你定義了一個(gè)最小高度(minHeight)同時(shí)enterAlways也定義了,那么view將在到達(dá)這個(gè)最小高度的時(shí)候開始顯示,并且從這個(gè)時(shí)候開始慢慢展開,當(dāng)滾動(dòng)到頂部的時(shí)候展開完。
exitUntilCollapsed: 同樣顧名思義,這個(gè)flag時(shí)定義何時(shí)退出,當(dāng)你定義了一個(gè)minHeight,這個(gè)view將在滾動(dòng)到達(dá)這個(gè)最小高度的時(shí)候消失。
下面來通過flag為exitUntilCollapsed時(shí),來實(shí)現(xiàn)Toolbar的折疊顯示的效果。這個(gè)時(shí)候呢,我們把Toolbar直接放在CollapsingToolbarLayout下,先修改appbar_main.xml布局如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout 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="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="248dp" app:expandedTitleMarginEnd="10dp" app:expandedTitleMarginStart="10dp" 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:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:background="?attr/colorPrimary" app:layout_collapseMode="pin"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout>
效果圖:

下面再來看一個(gè)更好玩高級的效果,實(shí)現(xiàn)滑動(dòng)的時(shí)候差生視覺差的感覺。先看效果圖:

先appbar_main.xml的布局如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout 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="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="248dp" app:expandedTitleMarginEnd="10dp" app:expandedTitleMarginStart="10dp" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" app:layout_collapseMode="parallax" android:background="@drawable/bg"/> <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>
實(shí)現(xiàn)視覺差的屬性主要來自于app:layout_collapseMode="parallax",這個(gè)flag代表的是視差模式,即在折疊的時(shí)候會有視差折疊的效果,而“pin”,固定模式,就是在折疊的最后固定在最頂端。
上面說了那么多,其實(shí)這些效果的實(shí)現(xiàn)都離不開一個(gè)東西,那就是Behavior。CoordinatorLayout的工作原理是搜索定義了CoordinatorLayout Behavior的子view,不管是通過在xml中使用app:layout_behavior標(biāo)簽還是通過在代碼中對view類使用@DefaultBehavior修飾符來添加注解。當(dāng)滾動(dòng)發(fā)生的時(shí)候,CoordinatorLayout會嘗試觸發(fā)那些聲明了依賴的子view。要自己定義CoordinatorLayoutBehavior,你需要實(shí)現(xiàn)layoutDependsOn() 和onDependentViewChanged()兩個(gè)方法。
綜上,差不就是CoordinatorLayout 的實(shí)現(xiàn)各種效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選