iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果
  • 563
分享到

怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果

coordinatorlayoutandroidroi 2023-05-31 09:05:22 563人浏览 独家记忆
摘要

本篇文章为大家展示了怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在Material Design里,Coordi

本篇文章为大家展示了怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

在Material Design里,CoordinatorLayout通常用来作为顶层视图,来协调处理各个子View之间的动作,从而实现各种动画效果,如Snackbar与FloatingActionButton的配合显示效果,就是以CoordinatorLayout作为根布局来实现的

CoordinatorLayout提供Behaviors接口,子View通过实现Behaviors接口来协调和其它View之间的显示效果,可以这么理解:

CoordinatorLayout让其子View之间互相知道彼此的存在,任意一个子View的状态变化会通过Behaviors通知其它子View,CoordinatorLayout就像一个桥梁,连接不同的View,并使用Behavior处理各个子View之间的通信

在xml布局文件中进行设置

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout  xmlns:android="Http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  xmlns:app="http://schemas.android.com/apk/res-auto">  <!--包裹头部View实现滑动效果-->  <android.support.design.widget.AppBarLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:theme="@style/ThemeOverlay.AppCompat">    <!--标题栏-->    <android.support.v7.widget.Toolbar      android:id="@+id/tb_toolbar"      android:layout_width="match_parent"      android:layout_height="wrap_content"      app:navigationIcon="@android:drawable/ic_dialog_email"      app:title="Title"      app:layout_scrollFlags="scroll" />    <!--Tab导航栏-->    <android.support.design.widget.TabLayout      android:id="@+id/tab_layout"      android:layout_width="match_parent"      android:layout_height="wrap_content"      app:tabMode="fixed"      app:layout_scrollFlags="scroll|enterAlways"/>  </android.support.design.widget.AppBarLayout>  <android.support.v4.view.ViewPager    android:id="@+id/vp_tab_pager"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"/></android.support.design.widget.CoordinatorLayout>

首先给被AppBarLayout包裹的View添加app:layout_scrollFlags属性,并设置相应的值

• scroll:让该View可以滚动出屏幕

• enterAlways:不需要滚动到顶部,只要有向上滚动的事件就显示该View

• enterAlwaysCollapsed:定义该View何时进入,如果你定义了一个最小高度minHeight,同时enterAlways也定义了,那么该View将会在到达这个最小高度的时候开始慢慢显示,直到滚动组件滚动到顶部时再完全展开

• exitUntilCollapsed:定义该View何时退出,如果你定义了一个最小高度minHeight,那么这个View将在滚动到达这个最小高度时消失

如果不设置layout_scrollFlags属性,那么该View就会固定在屏幕上

enterAlwaysCollapsed和exitUntilCollapsed属性主要是在搭配CollapsingToolbarLayout时使用的

注意:布局的时候,AppBarLayout里面滚动的View要放在固定的View上面

然后在CoordinatorLayout布局里放一个可以滚动的View(不支持ListView),这里使用ViewPager里放置一个RecylerView,为该ViewPager设置app:layout_behavior属性,这里可直接使用Android自带的值

app:layout_behavior=”@string/appbar_scrolling_view_behavior”

设置该值的作用相当于告诉CoordinatorLayout,此View是一个滚动控件,如果该View滚动了,那么设置了layout_scrollFlags属性的控件就可以响应滚动事件了

想实现效果一,总结

使用CoordinatorLayout作为根布局

使用AppBarLayout包裹头部View,并给需要滚动的View设置app:layout_scrollFlags属性

给滑动组件设置app:layout_behavior属性

效果二:

使用到CollapsingToolbarLayout布局,我们在效果一的基础上更改布局代码

<?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"  android:layout_width="match_parent"  android:layout_height="match_parent">  <android.support.design.widget.AppBarLayout    android:layout_width="match_parent"    android:layout_height="300dp"    android:fitsSystemwindows="true"    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">    <android.support.design.widget.CollapsingToolbarLayout      android:id="@+id/collapsing_toolbar"      android:layout_width="match_parent"      android:layout_height="match_parent"      app:contentScrim="?attr/colorPrimary"      app:expandedTitleMarginEnd="88dp"      app:expandedTitleMarginStart="66dp"      app:layout_scrollFlags="scroll|exitUntilCollapsed">      <!--拉开后显示的背景图片-->      <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:src="@drawable/bg_image"        app:layout_collapseMode="pin"/>      <!--标题栏-->      <android.support.v7.widget.Toolbar        android:id="@+id/tb_toolbar"        android:layout_width="match_parent"        android:layout_height="?attr/actionBarSize"        app:layout_collapseMode="pin"        app:navigationIcon="@android:drawable/ic_dialog_email"        app:title="Title"/>    </android.support.design.widget.CollapsingToolbarLayout>  </android.support.design.widget.AppBarLayout>  <android.support.v7.widget.RecyclerView    android:id="@+id/rv_data"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"/></android.support.design.widget.CoordinatorLayout>

一些属性的作用

• title:设置Toolbar的标题,注意:如果在CollapsingToolbarLayout中指定了title属性,那么Toolbar中的title属性将会变得无效

• expandedTitleMarginStart:设置下拉伸缩完成后,ToolBar标题文字左边的margin距离

• expandedTitleMarginEnd:设置下拉伸缩完成后,Toolbar标题文字右边的margin距离

• contentScrim:设置Toolbar折叠到顶部后的背景

• layout_collapseMode:折叠效果,有两个值,pin代表从底部拉出,parallax代表从中间展开

总结

首先我们设置一个固定的高度给AppBarLayout

然后给AppBarLayout的子View包裹了一层CollapsingToolbarLayout,并设置CollapsingToolbarLayout的滚动属性为scroll|exitUntilCollapsed

最后再为CollapsingToolbarLayout里的子View设置layout_collapseMode属性,指定其展示效果

上述内容就是怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。

--结束END--

本文标题: 怎么在Android应用中利用CoordinatorLayout实现一个标题滚动效果

本文链接: https://www.lsjlt.com/news/224665.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作