1、作用
类似于LinearLayout,默认是垂直布局。作用是1、将所有包裹的子View当成整个布局的Appbar;2、如果其他可以滚动的布局上下滑动时,Appbar里面包裹的内容如何处理。(可以跟着向上隐藏,也可以不动)
2、使用
见代码:
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
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"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:popupTheme="@style/AppBarPopStyle"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/AppBarStyle">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:text="@string/text"
android:textSize="30sp" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
1、重要属性释义:
app:layout_scrollFlags="scroll|enterAlways":AppbarLayout的子View如果需要在屏幕上下滑动时选择是滚出屏幕还是留在屏幕上时,就要设置此属性来判断。当选择“scroll”时,表示此View需要滚出屏幕;“scroll|enterAlways”表示向上滑动时会逐渐滚出屏幕,但只要向下滑动的动作就会慢慢出现在屏幕上;“scroll|exitUntilCollapsed”表示子View向上滑动时会逐渐移除屏幕直到子View的最小高度为止;"scroll|enterAlwaysCollapsed"表示子View向上移动时会移除屏幕外,向下滑时直到滑动到scrollView的最上面子View才会下滑。
app:layout_behavior:滑动的View和AppbarLayout的控件位置交互时的处理。
2、AppbarLayout的子View需要实现滑动效果的必要条件:
1、CoordinatorLayout作为AppbarLayout父布局容器
2、AppbarLayout的子View添加app:layout_scrollFlags属性
3、滚动的View实现添加app:layout_behavior属性
3、参考博客:
http://blog.csdn.net/abbott_133/article/details/51785619
http://www.jianshu.com/p/d159f0176576