Android Material Design Android官方控件學習
CoordinatorLayout
- CoordinatorLayout is a super-powered FrameLayout.
- CoordinatorLayout是一個增強型的FrameLayout,所以用法跟FrameLayout一樣的。
- 通過自定義子佈局的Behaviors(行爲)來實現控件之間的交互動畫效果,這是不同於FrameLayout的一點,跟其他MD控件(如下面要將的AppBarLayout等)一起使用,實現MD風格交互效果
導入design庫
'com.android.support:design:26.1.0'
先介紹跟FrameLayout一樣的用法
- 結合Toolbar和NestedScrollView
- NestedScrollView需要加上android:layout_marginTop="?actionBarSize",不讓會覆蓋Toolbar,果然就是FrameLayout的特性
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="?attr/colorPrimary"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize"
>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:text="這裏簡單展示跟FrameLayout一樣的用法,增強behavior用法請看下面AppBarLayoutActivity等"
android:background="#cdcdcd"
android:layout_height="400dp"/>
<TextView
android:layout_width="match_parent"
android:background="#ababab"
android:layout_height="400dp"/>
<TextView
android:layout_width="match_parent"
android:background="#fafafa"
android:layout_height="400dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
不使用Behaviors的CoordinatorLayout跟鹹魚一樣
下面將AppBarLayout時,你纔會發現CoordinatorLayout的神奇力量.
to be continue…