安卓Behavior用法
原帖: link
一個神奇的控件——Android CoordinatorLayout與Behavior使用指南
簡名關注42016.07.07 18:36:10字數 1,086閱讀 72,577CoordinatorLayout是support.design包中的控件,它可以說是Design庫中最重要的控件。
本文通過模仿知乎介紹了自定義Behavior,通過模仿百度地圖介紹了BottomSheetBehavior的使用。
1.CoordinatorLayout介紹
官方對CoordinatorLayout的描述是這樣的:
CoordinatorLayout is a super-powered FrameLayout.
CoordinatorLayout is intended for two primary use cases:
- As a top-level application decor or chrome layout
- As a container for a specific interaction with one or more child views
官方對Behavior的描述是這樣的:
Interaction behavior plugin for child views of CoordinatorLayout.
簡單來說,CoordinatorLayout是用來協調其子view們之間動作的一個父view,而Behavior就是用來給CoordinatorLayout的子view們實現交互的。
CoordinatorLayout在我之前的文章中都有出鏡:
沒時間解釋了,快使用Snackbar!——Android Snackbar花式使用指南中Snackbar顯示時FloatingActionButton相應上移為其留出位置
Snackbar與FloatingActionButton的互動看,這個工具欄能伸縮折疊——Android CollapsingToolbarLayout使用介紹中CollapsingToolbarLayout折疊或展開時,FloatingActionButton跟隨運動并且大小相應變化:
AppBarLayout與FloatingActionButton的互動看下FloatingActionButton的源碼就能發現其中有一個Behavior方法繼承自CoordinatorLayout.Behavior,并在其中實現了與Snackbar互動時的邏輯。
我本文使用的support:design版本是23.4.0
本文圖片接口來自干貨集中營http://gank.io/api
2.自定義Behavior模仿知乎
知乎的效果.gif 本文實現的效果.gif先看下布局
<?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/behavior_demo_coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent">總結
以上是生活随笔為你收集整理的安卓Behavior用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TF2.0 TFRecord创建和读取
- 下一篇: 我还是不用百度免费的CDN好了!