CollapsingToolbarLayout使用(可以折叠的Toolbar)

2023-12-07 13:48

本文主要是介绍CollapsingToolbarLayout使用(可以折叠的Toolbar),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CollapsingToolbarLayout作用是提供了一个可以折叠的Toolbar,它继承至FrameLayout,给它设置layout_scrollFlags,它可以控制包含在CollapsingToolbarLayout中的控件(如:ImageView、Toolbar)在响应layout_behavior事件时作出相应的scrollFlags滚动事件(移除屏幕或固定在屏幕顶端)。

使用CollapsingToolbarLayout:

[plain]  view plain copy
print ?
  1. <android.support.design.widget.AppBarLayout  
  2.         android:layout_width="match_parent"  
  3.         android:layout_height="256dp"  
  4.         android:fitsSystemWindows="true">  
  5.         <android.support.design.widget.CollapsingToolbarLayout  
  6.             android:id="@+id/collapsing_toolbar_layout"  
  7.             android:layout_width="match_parent"  
  8.             android:layout_height="match_parent"  
  9.             app:contentScrim="#30469b"  
  10.             app:expandedTitleMarginStart="48dp"  
  11.             app:layout_scrollFlags="scroll|exitUntilCollapsed">  
  12.   
  13.             <ImageView  
  14.                 android:layout_width="match_parent"  
  15.                 android:layout_height="match_parent"  
  16.                 android:scaleType="centerCrop"  
  17.                 android:src="@mipmap/bg"  
  18.                 app:layout_collapseMode="parallax"  
  19.                 app:layout_collapseParallaxMultiplier="0.7"  />  
  20.   
  21.             <android.support.v7.widget.Toolbar  
  22.                 android:id="@+id/toolbar"  
  23.                 android:layout_width="match_parent"  
  24.                 android:layout_height="?attr/actionBarSize"  
  25.                 app:layout_collapseMode="pin" />  
  26.         </android.support.design.widget.CollapsingToolbarLayout>  
  27.     </android.support.design.widget.AppBarLayout>  
我们在CollapsingToolbarLayout中设置了一个ImageView和一个Toolbar。并把这个CollapsingToolbarLayout放到AppBarLayout中作为一个整体。

1、在CollapsingToolbarLayout中:

我们设置了layout_scrollFlags:关于它的值我这里再说一下:

  • scroll - 想滚动就必须设置这个。
  • enterAlways - 实现quick return效果, 当向下移动时,立即显示View(比如Toolbar)。
  • exitUntilCollapsed - 向上滚动时收缩View,但可以固定Toolbar一直在上面。
  • enterAlwaysCollapsed - 当你的View已经设置minHeight属性又使用此标志时,你的View只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。
其中还设置了一些属性,简要说明一下:
  • contentScrim - 设置当完全CollapsingToolbarLayout折叠(收缩)后的背景颜色。
  • expandedTitleMarginStart - 设置扩张时候(还没有收缩时)title向左填充的距离。
没扩张时候如图:

2、在ImageView控件中:

我们设置了:

  • layout_collapseMode (折叠模式) - 有两个值:
    • pin -  设置为这个模式时,当CollapsingToolbarLayout完全收缩后,Toolbar还可以保留在屏幕上。
    • parallax - 设置为这个模式时,在内容滚动时,CollapsingToolbarLayout中的View(比如ImageView)也可以同时滚动,实现视差滚动效果,通常和layout_collapseParallaxMultiplier(设置视差因子)搭配使用。
  • layout_collapseParallaxMultiplier(视差因子) - 设置视差滚动因子,值为:0~1。 
3、在Toolbar控件中:
我们设置了layout_collapseMode(折叠模式):为pin。

综上分析:当设置了layout_behavior的控件响应起了CollapsingToolbarLayout中的layout_scrollFlags事件时,ImageView会有视差效果的向上滚动移除屏幕,当开始折叠时CollapsingToolbarLayout的背景色(也就是Toolbar的背景色)就会变为我们设置好的背景色,Toolbar也一直会固定在最顶端。
效果如图:


【注】:使用CollapsingToolbarLayout时必须把title设置到CollapsingToolbarLayout上,设置到Toolbar上不会显示。即:
mCollapsingToolbarLayout.setTitle(" ");
该变title的字体颜色:
扩张时候的title颜色:mCollapsingToolbarLayout.setExpandedTitleColor();
收缩后在Toolbar上显示时的title的颜色:mCollapsingToolbarLayout.setCollapsedTitleTextColor();
这个颜色的过度变化其实CollapsingToolbarLayout已经帮我们做好,它会自动的过度,比如我们把收缩后的title颜色设为绿色,效果如图:


没录好,反正效果出来了。
接下来看看代码怎么实现吧:
布局文件:
[html]  view plain copy
print ?
  1. <android.support.design.widget.CoordinatorLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context=".MainActivity">  
  8.   
  9.     <android.support.design.widget.AppBarLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="256dp"  
  12.         android:fitsSystemWindows="true">  
  13.         <android.support.design.widget.CollapsingToolbarLayout  
  14.             android:id="@+id/collapsing_toolbar_layout"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="match_parent"  
  17.             app:contentScrim="#30469b"  
  18.             app:expandedTitleMarginStart="48dp"  
  19.             app:layout_scrollFlags="scroll|exitUntilCollapsed">  
  20.   
  21.             <ImageView  
  22.                 android:layout_width="match_parent"  
  23.                 android:layout_height="match_parent"  
  24.                 android:scaleType="centerCrop"  
  25.                 android:src="@mipmap/bg"  
  26.                 app:layout_collapseMode="parallax"  
  27.                 app:layout_collapseParallaxMultiplier="0.7"  />  
  28.   
  29.             <android.support.v7.widget.Toolbar  
  30.                 android:id="@+id/toolbar"  
  31.                 android:layout_width="match_parent"  
  32.                 android:layout_height="?attr/actionBarSize"  
  33.                 app:layout_collapseMode="pin" />  
  34.         </android.support.design.widget.CollapsingToolbarLayout>  
  35.     </android.support.design.widget.AppBarLayout>  
  36.   
  37.     <LinearLayout  
  38.         android:layout_width="match_parent"  
  39.         android:layout_height="match_parent"  
  40.         android:orientation="vertical"  
  41.         app:layout_behavior="@string/appbar_scrolling_view_behavior">  
  42.         <android.support.v7.widget.RecyclerView  
  43.             android:id="@+id/recyclerView"  
  44.             android:layout_width="match_parent"  
  45.             android:layout_height="match_parent"  
  46.             android:scrollbars="none" />  
  47.     </LinearLayout>  
  48. </android.support.design.widget.CoordinatorLayout>  

代码文件:
[java]  view plain copy
print ?
  1. Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);  
  2.     setSupportActionBar(mToolbar);  
  3.     getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
  4.     mToolbar.setNavigationOnClickListener(new View.OnClickListener() {  
  5.         @Override  
  6.         public void onClick(View v) {  
  7.             onBackPressed();  
  8.         }  
  9.     });  
  10.     //使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上,设置到Toolbar上则不会显示  
  11.     CollapsingToolbarLayout mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);  
  12.     mCollapsingToolbarLayout.setTitle("CollapsingToolbarLayout");  
  13.     //通过CollapsingToolbarLayout修改字体颜色  
  14.     mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//设置还没收缩时状态下字体颜色  
  15.     mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);//设置收缩后Toolbar上字体的颜色  

源码:http://download.csdn.net/detail/u010687392/8906931

这篇关于CollapsingToolbarLayout使用(可以折叠的Toolbar)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/466114

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四