01 相關概念
CoordinateLayout
AppbarLayout
屬性 layout_scrollFlags
behavior
02 相關代碼
佈局文件activity_coordinate_layout_demo.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.youyou.demo_ui.CoordinateLayoutDemoActivity">
<!--appbarlayout裏邊的東西可以隱藏 -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitCenter"
android:src="@drawable/account_head_default"
app:layout_scrollFlags="scroll" />
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="@dimen/dp_size_10"
android:gravity="center"
android:text="設置標題"
android:textAlignment="center"
android:textSize="20sp" />
</android.support.design.widget.AppBarLayout>
<!--設置layout behavior讓可以滑動的控件和scrollview聯動 -->
<!-- scrollview不行 nestedScrollview可以 爲啥? -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green_weixin_btn">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/grey">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/green_4DC0A4">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/grey">
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
界面效果: