Android4.4沉浸状态栏结合CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout等使用详解

来源:互联网 发布:php开发篮球网站源码 编辑:程序博客网 时间:2024/05/16 01:53

现在我们要把它结合CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout等一起来使用!

先上效果图



布局文件

[html] view plain copy
  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:fitsSystemWindows="true">  
  6.   
  7.     <android.support.design.widget.AppBarLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="180dp"  
  10.         android:fitsSystemWindows="true">  
  11.   
  12.         <android.support.design.widget.CollapsingToolbarLayout  
  13.             android:id="@+id/collapsing_toolbar"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="match_parent"  
  16.             app:expandedTitleMarginEnd="64dp"  
  17.             app:contentScrim="?attr/colorPrimary"  
  18.             app:expandedTitleMarginStart="48dp"  
  19.             app:statusBarScrim="?attr/colorPrimary"  
  20.             app:layout_scrollFlags="scroll|exitUntilCollapsed"  
  21.             android:fitsSystemWindows="true">  
  22.   
  23.             <ImageView  
  24.                 android:layout_width="match_parent"  
  25.                 android:layout_height="200dp"  
  26.                 android:scaleType="fitXY"  
  27.                 android:src="@drawable/yuwenle"  
  28.                 app:layout_collapseParallaxMultiplier="0.6"  
  29.                 app:layout_collapseMode="parallax"/>  
  30.   
  31.             <android.support.v7.widget.Toolbar  
  32.                 xmlns:android="http://schemas.android.com/apk/res/android"  
  33.                 android:id="@+id/tl_custom"  
  34.                 android:layout_width="match_parent"  
  35.                 android:layout_height="?attr/actionBarSize"  
  36.                 android:fitsSystemWindows="true"  
  37.                 app:layout_scrollFlags="scroll|enterAlways"  
  38.                 app:layout_collapseMode="pin"  
  39.                 >  
  40.             </android.support.v7.widget.Toolbar>  
  41.   
  42.         </android.support.design.widget.CollapsingToolbarLayout>  
  43.   
  44.    </android.support.design.widget.AppBarLayout>  
  45.   
  46.     <android.support.v7.widget.RecyclerView  
  47.         android:id="@+id/rvId"  
  48.         android:layout_width="match_parent"  
  49.         android:layout_height="match_parent"  
  50.         app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  51.         android:paddingTop="10dp">  
  52.   
  53.     </android.support.v7.widget.RecyclerView>  
  54.   
  55.     <android.support.design.widget.FloatingActionButton  
  56.         android:id="@+id/fabId"  
  57.         android:layout_width="wrap_content"  
  58.         android:layout_height="wrap_content"  
  59.         android:src="@android:drawable/sym_action_email"  
  60.         android:layout_gravity="bottom|end"  
  61.         android:layout_margin="10dp"  
  62.         >  
  63.     </android.support.design.widget.FloatingActionButton>  
  64.   
  65. </android.support.design.widget.CoordinatorLayout>  

CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout的属性和使用参考

Android5.x新特性之Toolbar,AppBarLayout,CoordinatorLayout,CollapsingToolbarLayout等汇总


这里要注意的有:

1.最外层的CoordinatorLayout一定要有fitsSystemWindows="true"

2.Toolbar的height要使用?actionBarSize,而不能使用上一篇所说的wrap_content了,因为使用了wrap_content,就会导致Toolbar不能滑动


阅读全文
0 0