美文网首页
【Android】Material Toolbar

【Android】Material Toolbar

作者: Wavky | 来源:发表于2017-03-02 15:48 被阅读0次

通过 CoordinateLayout + AppBarLayout + CollapsingToolbarLayout 组装成一个符合 Material 动画表现的复合 Toolbar。

结构

<CoordinatorLayout>  // 提供子类相互关联同步运动功能
    <AppBarLayout>      // 组装一个合成的 App bar,并提供基本的同步滚动功能实现
        <CollapsingToolbarLayout> // 提供 Toolbar 专用的 Material 折叠、形变动画
            <Background View />
            <Toolbar 
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />
        </CollapsingToolbarLayout>
        <Other part belong to app bar />  // 同步滚动功能对其他�子View 无效
    </AppBarLayout>
    
    <NestedScrollingChild />     // 提供滚动参考数据
    ...
</CoordinatorLayout>

CoordinatorLayout

提供 View 之间的基础联动交互功能

布局方式与 FrameLayout 相同,可协调(关联)内部任意某些 子View(指定app:layout_behavior属性,aka "ChildView")跟随另一个任意特定 View(aka "DependencyView")的尺寸、位置变化或被移除、手势交互等动作时,做相对变化响应(需要自定义Behavior处理器并设置到上面的属性中)。

关于 anchor

子View 可指定任意另一个 CoordinatorLayout 内的子View作为其锚定(anchor)的对象,用于布置浮动跟随型的View,此时 子View 将跟随其 anchor 指定的 anchoredView 移动。
子View 不能锚定自己本身,也不能锚定其 子View(会形成环形依赖)。

属性

Constructor部分,用于本体

  • statusBarBackground:状态栏背景 Drawable

LayoutParams部分,用于子元素

  • layout_behavior:用于 ChildView 上,指定当前 View 相对于其他 View 的相对动作响应处理器
  • layout_anchor:指定一个锚定对象View
  • layout_anchorGravity:指定锚定跟随位置,默认为目标对象的左上角
  • layout_insetEdge:该子View 从哪个方向插入 CoordinatorLayout 容器
  • layout_dodgeInsetEdges:该子View 回避下一个同方向插入的 View 的移动方向

Behavior

用于指定当前View(Child)相对于其他View(Dependency)作出自定义响应的逻辑脚本监听响应类


AppBarLayout

提供基础关联滚动实现

LinearLayout 子类容器,用于垂直串联组装一个逻辑上的 AppBar(不限定使用 Toolbar),内部指定了默认Behavior实现,可让(第一个)子View(通过app:layout_scrollFlags属性值指定)跟随外部的 ScrollableView(NestedScrollingChild子类)滚动动作而做特定滚动动作。

通常需要在 NestedScrollingChild(或其父容器)上指定app:layout_behavior="@string/appbar_scrolling_view_behavior",使该 View 在 CoordinatorLayout 中正确布局在 AppBarLayout 下方,并随 AppBarLayout 滚动做相对位移。

属性

Constructor部分,用于本体(仅在 CoordinatorLayout 内生效)

  • expanded:初始化时是否以扩展形式展示

LayoutParams部分,仅用于第一个子元素

  • layout_scrollFlags:指定联动滚动的表现方式
    • scroll:作为头部,与目标 View 的 scroll 动作同步滚动,该值作为下面其他属性的前提条件联合使用,如app:layout_scrollFlags="scroll|...
    • enterAlways:scroll 下滑动作发生时,立即触发 AppBar 下滑弹出
    • enterAlwaysCollapsed:与enterAlways配套,AppBar 仅弹出一个最小高度,直至 scroll 下滑至顶端时,再继续下滑弹出完整的 AppBar
    • exitUntilCollapsed:scroll 上滑动作发生时,AppBar 仅上滑收缩至一个最小高度(普通 Toolbar 高度),不完全隐藏
    • snap:AppBar 作为一个整体完整出现或隐藏,并提供弹性动画效果
  • layout_scrollInterpolator:指定动画曲线插值器

CollapsingToolbarLayout

提供 Material 式的关联滚动动画实现(依赖 AppBarLayout)

FrameLayout 子类,用于包裹(层叠) Toolbar 及其 背景View 等部件,(通过指定各种app:属性值)提供符合 Material Design 规范的 Toolbar 及其背景的折叠拉伸、遮罩、视差动画等视觉效果。

需要放置在 AppBarLayout 下作为直接子View使用(可复数并列使用)。

使用方式可参考 玩转AppBarLayout

属性

Constructor部分,用于本体

  • collapsedTitleGravityexpandedTitleGravity:缩放后 Title 的对齐方式
    • center、center_vertical、center_horizontal
    • start、end
    • top、bottom、left、right
    • fill_vertical
  • expandedTitleMarginexpandedTitleMarginStartexpandedTitleMarginEndexpandedTitleMarginTopexpandedTitleMarginBottom:Title 扩展后所需的四边 margin
  • collapsedTitleTextAppearanceexpandedTitleTextAppearance:缩放后 Title 的 style 形式(颜色和大小)
  • contentScrim:内容遮罩,Toolbar 折叠完毕后覆盖原背景图的新背景颜色
  • statusBarScrim:状态栏背景遮罩,要求 API 21 并设置 fitsSystemWindows
  • scrimVisibleHeightTrigger:触发遮罩效果的最小可视高度(带单位)
  • scrimAnimationDuration:遮罩动画时长
  • titleEnabled:是否启用动态缩放 Title
  • title:Toolbar 折叠后显示的标题文字,会根据折叠程度缩放大小
  • toolbarId:指定应用动画效果的 Toolbar 组件id,不指定则以第一个搜索到的 Toolbar 为准

另外可通过代码独立指定 Title 的颜色、字体、遮罩图像、遮罩及其动画开闭等效果。

LayoutParams部分,用于直接子元素

  • layout_collapseMode
    Bar发生联动滚动折叠时,子View的动作模式
    • none:不折叠
    • pin:折叠模式下 Toolbar 保留在顶部位置
    • parallax:视差跟随滚动
  • layout_collapseParallaxMultiplier
    视差浮点系数,控制视差滚动的偏移量,负数表示加速滚动,0 表示完全跟随滚动(没有视差效果),1f 表示完全逆向滚动(相对静止状态),>1f 表示逆向加速滚动,默认值为0.5,源码视差偏移算法简约为offset = Math.round(-verticalOffset * parallaxMult),其中verticalOffset表示 scroll 的垂直滚动量

参考

相关文章

网友评论

      本文标题:【Android】Material Toolbar

      本文链接:https://www.haomeiwen.com/subject/yickgttx.html