欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解android特性之CoordinatorLayout用法探析實(shí)例

 更新時間:2018年02月01日 09:15:03   作者:FightSeeker  
本篇文章主要介紹了android特性之CoordinatorLayout用法探析實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

當(dāng)我在AS上新建一個module時,系統(tǒng)默認(rèn)的最外層布局不再是我們熟悉的五大布局中的一種,而是一個全新的布局:CoordinatorLayout。它是Material風(fēng)格的重要組件, 作為布局的頂層控件,協(xié)調(diào)(Coordinate)其他組件, 實(shí)現(xiàn)聯(lián)動。

下面來看一個最簡單的例子,CoordinatorLayout與FloatingActionButton的使用,它可以使浮動按鈕上下移動,為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顯示一下即可。那么,效果圖就像下面展示的莪一樣:


接下來看一個高級點(diǎn)的效果,就是標(biāo)題欄,也就是ToolBar的擴(kuò)展與收縮效果。要想要ToolBar響應(yīng)滾動事件,這里我們需要用到一個控件:AppBarLayout,這個控件必須作為CoordinatorLayout的直接子View,才會響應(yīng)滾動事件。首先因?yàn)槲覀兊腡ooBar是需要響應(yīng)滾動的視圖,所以需要為其配置一個屬性:layout_scrollFlags。然后呢,我們需要定義一下AppBarLayout與滾動視圖(如RecyclerView,NestedScrollView等可以支持嵌套滾動的控件)supportlibrary包含了一個特殊的字符串資源@string/appbar_scrolling_view_behavior,它和AppBarLayout.ScrollingViewBehavior相匹配,用來通知AppBarLayout 這個特殊的view何時發(fā)生了滾動事件,這個behavior需要設(shè)置在觸發(fā)事件(滾動)的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)視圖在滾動的時候,ToolBar滾出了屏幕,為內(nèi)容區(qū)域留出了更大空間。其中控制ToolBar是否可以滾出屏幕的屬性是由app:layout_scrollFlags="scroll|enterAlways"。來說一下這個屬性,要想滾出屏幕layout_scrollFlags必須設(shè)置scrll這個flag。剩下的幾個flag解釋如下:

enterAlways:只要滾動視圖向下滾動,view就會顯示出來。

enterAlwaysCollapsed:顧名思義,這個flag定義的是何時進(jìn)入(已經(jīng)消失之后何時再次顯示)。假設(shè)你定義了一個最小高度(minHeight)同時enterAlways也定義了,那么view將在到達(dá)這個最小高度的時候開始顯示,并且從這個時候開始慢慢展開,當(dāng)滾動到頂部的時候展開完。

exitUntilCollapsed: 同樣顧名思義,這個flag時定義何時退出,當(dāng)你定義了一個minHeight,這個view將在滾動到達(dá)這個最小高度的時候消失。

下面來通過flag為exitUntilCollapsed時,來實(shí)現(xiàn)Toolbar的折疊顯示的效果。這個時候呢,我們把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> 

效果圖:


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


先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",這個flag代表的是視差模式,即在折疊的時候會有視差折疊的效果,而“pin”,固定模式,就是在折疊的最后固定在最頂端。

上面說了那么多,其實(shí)這些效果的實(shí)現(xiàn)都離不開一個東西,那就是Behavior。CoordinatorLayout的工作原理是搜索定義了CoordinatorLayout Behavior的子view,不管是通過在xml中使用app:layout_behavior標(biāo)簽還是通過在代碼中對view類使用@DefaultBehavior修飾符來添加注解。當(dāng)滾動發(fā)生的時候,CoordinatorLayout會嘗試觸發(fā)那些聲明了依賴的子view。要自己定義CoordinatorLayoutBehavior,你需要實(shí)現(xiàn)layoutDependsOn() 和onDependentViewChanged()兩個方法。

綜上,差不就是CoordinatorLayout 的實(shí)現(xiàn)各種效果了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論