CollapsingToolbarLayout详解

一、介绍

CollapsingToolbarLayout是Android Material Design库中的一个布局控件,主要用于实现带折叠效果的Toolbar。

它可以在Toolbar上面加载大的图片或者内容,当向下滚动时,这些内容会向下折叠,并最终将整个Toolbar收缩起来。 CollapsingToolbarLayout通常与AppBarLayout、NestedScrollView或RecyclerView等其他控件一起使用,实现类似于网易云音乐、知乎等APP的样式,提供了一种优雅、简洁的设计风格。

二、属性详解

1. app:contentScrim

作用:设置当CollapsingToolbarLayout折叠后的Toolbar颜色为透明时,覆盖在其上方的一个Drawable。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:contentScrim="@color/colorPrimary"
       ...>

2. app:scrimAnimationDuration

作用:设置折叠动画的触发时间。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:scrimAnimationDuration="500"
       ...>

3. app:expandedTitleMarginStart

作用:设置标题展开时距离左边屏幕的margin值。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:expandedTitleMarginStart="48dp"
       ...>

4. app:expandedTitleMarginBottom

作用:设置标题展开时距离底部的margin值。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:expandedTitleMarginBottom="48dp"
       ...>

5. app:expandedTitleTextAppearance

作用:设置标题展开时的TextStyle。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedText"
       ...>

6. app:collapsedTitleTextAppearance

作用:设置标题折叠时的TextStyle。

   <android.support.design.widget.CollapsingToolbarLayout
       ...
       app:collapsedTitleTextAppearance="@style/CollapsingToolbarLayoutCollapsedText"
       ...>

三、样例代码

下面是一个简单的样例代码,演示如何使用CollapsingToolbarLayout:

   <android.support.design.widget.CoordinatorLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.AppBarLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content">

           <android.support.design.widget.CollapsingToolbarLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="@color/colorPrimary"
               app:contentScrim="@color/colorPrimaryDark"
               app:layout_scrollFlags="scroll|exitUntilCollapsed">

               <ImageView
                   android:layout_width="match_parent"
                   android:layout_height="200dp"
                   android:scaleType="centerCrop"
                   android:src="@drawable/image"
                   app:layout_collapseMode="parallax"/>

               <android.support.v7.widget.Toolbar
                   android:layout_width="match_parent"
                   android:layout_height="?attr/actionBarSize"
                   app:layout_collapseMode="pin"/>

           </android.support.design.widget.CollapsingToolbarLayout>

       </android.support.design.widget.AppBarLayout>

       <android.support.v4.widget.NestedScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:layout_behavior="@string/appbar_scrolling_view_behavior">

           <TextView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:text="@string/large_text"/>

       </android.support.v4.widget.NestedScrollView>

   </android.support.design.widget.CoordinatorLayout>

四、总结

CollapsingToolbarLayout是Android Material Design库中的一个非常实用的控件,用于实现带折叠效果的Toolbar。通过本篇文章的介绍,相信大家对于CollapsingToolbarLayout有了更加深入的理解,有助于大家在日常的Android应用开发中更加灵活地使用它。

原创文章,作者:PMFY,如若转载,请注明出处:https://www.506064.com/n/130963.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
PMFY的头像PMFY
上一篇 2024-10-03 23:42
下一篇 2024-10-03 23:42

相关推荐

  • Linux sync详解

    一、sync概述 sync是Linux中一个非常重要的命令,它可以将文件系统缓存中的内容,强制写入磁盘中。在执行sync之前,所有的文件系统更新将不会立即写入磁盘,而是先缓存在内存…

    编程 2025-04-25
  • 神经网络代码详解

    神经网络作为一种人工智能技术,被广泛应用于语音识别、图像识别、自然语言处理等领域。而神经网络的模型编写,离不开代码。本文将从多个方面详细阐述神经网络模型编写的代码技术。 一、神经网…

    编程 2025-04-25
  • C语言贪吃蛇详解

    一、数据结构和算法 C语言贪吃蛇主要运用了以下数据结构和算法: 1. 链表 typedef struct body { int x; int y; struct body *nex…

    编程 2025-04-25
  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • Python输入输出详解

    一、文件读写 Python中文件的读写操作是必不可少的基本技能之一。读写文件分别使用open()函数中的’r’和’w’参数,读取文件…

    编程 2025-04-25
  • Linux修改文件名命令详解

    在Linux系统中,修改文件名是一个很常见的操作。Linux提供了多种方式来修改文件名,这篇文章将介绍Linux修改文件名的详细操作。 一、mv命令 mv命令是Linux下的常用命…

    编程 2025-04-25
  • 详解eclipse设置

    一、安装与基础设置 1、下载eclipse并进行安装。 2、打开eclipse,选择对应的工作空间路径。 File -> Switch Workspace -> [选择…

    编程 2025-04-25
  • Java BigDecimal 精度详解

    一、基础概念 Java BigDecimal 是一个用于高精度计算的类。普通的 double 或 float 类型只能精确表示有限的数字,而对于需要高精度计算的场景,BigDeci…

    编程 2025-04-25
  • Python安装OS库详解

    一、OS简介 OS库是Python标准库的一部分,它提供了跨平台的操作系统功能,使得Python可以进行文件操作、进程管理、环境变量读取等系统级操作。 OS库中包含了大量的文件和目…

    编程 2025-04-25
  • nginx与apache应用开发详解

    一、概述 nginx和apache都是常见的web服务器。nginx是一个高性能的反向代理web服务器,将负载均衡和缓存集成在了一起,可以动静分离。apache是一个可扩展的web…

    编程 2025-04-25

发表回复

登录后才能评论