詳解android特性之CoordinatorLayout用法探析實(shí)例
當(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è)最簡(jiǎn)單的例子,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>
代碼就很簡(jiǎn)單了,如下:
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è)高級(jí)點(diǎn)的效果,就是標(biāo)題欄,也就是ToolBar的擴(kuò)展與收縮效果。要想要ToolBar響應(yīng)滾動(dòng)事件,這里我們需要用到一個(gè)控件:AppBarLayout,這個(gè)控件必須作為CoordinatorLayout的直接子View,才會(huì)響應(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就會(huì)顯示出來。
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è)更好玩高級(jí)的效果,實(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í)候會(huì)有視差折疊的效果,而“pin”,固定模式,就是在折疊的最后固定在最頂端。
上面說了那么多,其實(shí)這些效果的實(shí)現(xiàn)都離不開一個(gè)東西,那就是Behavior。CoordinatorLayout的工作原理是搜索定義了CoordinatorLayout Behavior的子view,不管是通過在xml中使用app:layout_behavior標(biāo)簽還是通過在代碼中對(duì)view類使用@DefaultBehavior修飾符來添加注解。當(dāng)滾動(dòng)發(fā)生的時(shí)候,CoordinatorLayout會(huì)嘗試觸發(fā)那些聲明了依賴的子view。要自己定義CoordinatorLayoutBehavior,你需要實(shí)現(xiàn)layoutDependsOn() 和onDependentViewChanged()兩個(gè)方法。
綜上,差不就是CoordinatorLayout 的實(shí)現(xiàn)各種效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android如何實(shí)現(xiàn)設(shè)備的異顯功能詳解
這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)設(shè)備的異顯功能的相關(guān)資料,這篇文章通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
android配合viewpager實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例分享
本文主要介紹了android實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例,配合viewpager作為標(biāo)簽欄,且可以設(shè)置每頁(yè)顯示的標(biāo)簽個(gè)數(shù),超出可滑動(dòng)顯示,需要的朋友可以參考下2014-02-02
Flutter應(yīng)用框架運(yùn)行微信小程序方法
這篇文章主要介紹了在Flutter?App內(nèi)運(yùn)行微信小程序的過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Android TelephonyManager詳解及實(shí)現(xiàn)代碼
本文主要介紹Android TelephonyManager, 這里整理了關(guān)于Android TelephoneManager的相關(guān)資料,并附有示例代碼和實(shí)現(xiàn)效果圖,有需要的朋友可以參考下2016-08-08
Android 有道詞典的簡(jiǎn)單實(shí)現(xiàn)方法介紹
本篇文章小編為大家介紹,Android 有道詞典的簡(jiǎn)單實(shí)現(xiàn)方法介紹。需要的朋友參考下2013-04-04
Android自定義EditText實(shí)現(xiàn)淘寶登錄功能
這篇文章主要為大家詳細(xì)介紹了Android自定義EditText實(shí)現(xiàn)淘寶登錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Intent 用法全面總結(jié)及實(shí)例代碼
這篇文章主要介紹了Android Intent 用法全面總結(jié)的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-09-09
Android App將數(shù)據(jù)寫入內(nèi)部存儲(chǔ)和外部存儲(chǔ)的示例
這篇文章主要介紹了Android App將數(shù)據(jù)寫入內(nèi)部存儲(chǔ)和外部存儲(chǔ)的示例,使用外部存儲(chǔ)即訪問并寫入SD卡,需要的朋友可以參考下2016-03-03
android引導(dǎo)用戶開啟自啟動(dòng)權(quán)限的方法
今天小編就為大家分享一篇android引導(dǎo)用戶開啟自啟動(dòng)權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07

