標籤:
Android Support Design Library是5.0的安卓支援庫,最低相容版本是Android 2.2,可以說是一個良心的產品,以前編寫Android程式總需要考慮到版本的問題,現在使用這個Android支援庫開發可以不需要考慮這類問題,這可以說是另一種開發語言,下面我們將一一介紹裡面的每個控制項的使用方式以及其擴充特效。
1.什麼是CoordinatorLayout
從其英文名字可知道其為“協調者”,組織“協調”子View的父View,其繼承自FrameLayout,其預設布局是一層一層往上疊加的,與FrameLayout一樣。
其神奇的地方就在於Behavior對象了。可以為任何View添加一個Behavior。Behavior是一系列回調。讓你有機會以非侵入的為View添加動態依賴布局,和處理父布局(CoordinatorLayout)滑動手勢的機會。
2.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:id="@+id/main_content"
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="256dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#30469b"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/biz_live_foot_bg"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
上面有如下屬性需要解釋:
①app:layout_scrollFlags有如下4個值:
——scroll - 想滾動就必須設定這個。
——enterAlways - 實現快速返回效果, 當向下移動時,立即顯示View(比如Toolbar)。 與scroll配合使用如下:
——exitUntilCollapsed - 向上滾動時收縮View,但可以固定Toolbar一直在上面。 與scroll配合使用如下:
——enterAlwaysCollapsed - 當你的View已經設定minHeight屬性又使用此標誌時,你的View只能以最小高度進入,只有當滾動視圖到達頂部時才擴大到完整高度。
②app:contentScrim:當CollapsingToolbarLayout摺疊(收縮)後的背景顏色。
③app:expandedTitleMarginStart與app:expandedTitleMarginEnd:圖片拉出與拉入,標題距離左邊的距離。之間就是移動的動畫距離
④app:layout_collapseMode的兩個取值:
——pin - 設定為這個模式時,當CollapsingToolbarLayout完全收縮後,Toolbar還可以保留在螢幕上。
——parallax - 設定為這個模式時,在內容滾動時,CollapsingToolbarLayout中的View(比如ImageView)也可以同時滾動,實現視差滾動效果,通常和layout_collapseParallaxMultiplier(設定視差因子)搭配使用。
⑤app:layout_collapseParallaxMultiplier:子視圖的視覺差
⑥app:layout_behavior="@string/appbar_scrolling_view_behavior":最重要的屬性,要實現聯動效果,必須設定聯動的滑動控制項,比如RecyclerView。這樣錄RecyclerView滾動的時候,標題列的動畫才有效果。
3.自訂一個Behavior
㈠繼承Behavior
public class LYJBehavior extends CoordinatorLayout.Behavior {
public LYJBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
㈡2種引用方法:
①在XML布局中直接引用
app:layout_behavior=“你的Behavior包含包名的類名”
②另外一種方法如果你的自訂View預設使用一個Behavior。在你的自訂View類上添加@DefaultBehavior(你的Behavior.class)這句註解。你的View就預設使用這個Behavior,代碼如下:
@DefaultBehavior(AppBarLayout.Behavior.class)
public class LYJLayout extends LinearLayout {}
㈢產生Behavior後第一件事就是確定依賴關係。重寫Behavior的這個方法來確定你依賴哪些View。
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return super.layoutDependsOn(parent, child, dependency);
}
child 是指應用behavior的View ,dependency 擔任觸發behavior的角色,並與child進行互動。確定你是否依賴於這個ViewCoordinatorLayout會將自己所有View遍曆判斷。如果確定依賴。這個方法很重要。
㈣當所依賴的View變動時會回調這個方法。
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
return super.onDependentViewChanged(parent, child, dependency);
}
4.使用自訂的Behavior
自訂一個屬性:
<declare-styleable name="Follow">
<attr name="target" format="reference"/>
</declare-styleab<strong>le></strong>
使用代碼:
public class LYJBehavior extends CoordinatorLayout.Behavior {
private int targetId;
public LYJBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Follow);
for (int i = 0; i < a.getIndexCount(); i++) {
int attr = a.getIndex(i);
if(a.getIndex(i) == R.styleable.Follow_target){
targetId = a.getResourceId(attr, -1);//擷取聯動ID
}
}
a.recycle();
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency.getId() == targetId;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
child.setY(dependency.getY()+dependency.getHeight());//child不管dependency怎麼移動,其都在dependency下面
return true;
}
}
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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<View
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="128dp"
android:background="@android:color/holo_blue_light"/>
<View
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="128dp"
app:layout_behavior="com.example.liyuanjing.tablayoutdemo.LYJBehavior"
app:target="@id/first"
android:background="@android:color/holo_green_light"/>
</android.support.design.widget.CoordinatorLayout>
app:target:中傳入ID,自訂behavior就會擷取聯動的View。然後根據你在onDependentViewChanged設定的聯動方式,進行聯動。
????
Android Support Design Library之CoordinatorLayout