CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout的用法,让Toolbar与系统栏融为一体

CoordinatorLayout其实是加强版的FrameLayout布局,可以监听期所有子控件的各种事件,由Design Support库提供的,能体现Material Design 的魔力。能解决其子控件互相遮挡问题。
所以,当Toolbar控件与RecycledView控件互相时,所以要放到CoordinatorLayout布局中。

AppBarLayout相当于垂直方向 的LinearLayout布局,它在内部做了很多滚动事件的封装,也应用了Material Design 的设计理念。由于AppBarLayout严重依赖CoordinatorLayout,所以可以让它内部的Toolbar与RecycledView
避免互相遮挡。
AppBarLayout来自design兼容包,使用需要添加依赖。android studio 添加依赖如下:依赖库必须是最新的。
implementation 'com.android.support:design:27.1.0'

 CollapsingToolbarLayout是一个作用在Toolbar基础之上的布局,它也是由Design Support库提供的,它让Toolbar的效果变得更丰富,不仅展示一个标题栏,而且加载图片。所以,修饰Toolbar的属性最好用来修饰CollapsingToolbarLayout,这样可以让效果最好,比如

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

 在上述三者的布局中加入ImageView控件,为了使Toolbar能系统状态栏,需要在上述四个布局代码中加入

android:fitsSystemWindows="true"

  另外还要在values/styles.xml文件中修改如下:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@android:color/transparent</item>//这是需要增加的属性,让系统栏透明,从而让Toolbar与系统栏融为一体
    </style>

  

 



原文地址:https://www.cnblogs.com/geili/p/10241439.html