CoordinatorLayout使用浅析

简介: CoordinatorLayout使用浅析

image.png

image.png


 



CoordinatorLayout是design包中的控件,作为外层控件可以协调子控件从而实现炫酷的效果等。



compile 'com.android.support:design:25.4.0'


image.png


在android studio中有一个ScrollingActivity的模板

image.png




<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.yechaoa.materialdesign.activity.ScrollingActivity">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <include layout="@layout/content_scrolling"/>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>


include:



<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.yechaoa.materialdesign.activity.ScrollingActivity"
    tools:showIn="@layout/activity_scrolling">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text"/>
</android.support.v4.widget.NestedScrollView>


外层是CoordinatorLayout,然后是AppBarLayout,然后是内容区和FloatingActionButton



这个AppBarLayout是有固定高度的,然后里面包着Toolbar,Toolbar外层是CollapsingToolbarLayout,是可折叠的



CollapsingToolbarLayout主要参数是app:layout_scrollFlags:


scroll :想要触发滑动事件,必须要加上这个属性,不加最后会停在顶端,比如Tablayout


exitUntilCollapsed :表示滑动到最后折叠在顶端


enterAlways :表示向上滑动消失,向下滑动可见



Toolbar主要参数app:layout_collapseMode:


pin :固定模式,在折叠的时候最后固定在顶端


parallax :视差模式,在折叠的时候会有个视差折叠的效果。



只有在内容区可滑动的状态下会触发效果,所以内容区外层是NestedScrollView,app:layout_behavior="@string/appbar_scrolling_view_behavior"属性触发滑动效果,


所以内容区需要可滑动且加上app:layout_behavior属性才能触发滑动效果,换成RecycleView加上app:layout_behavior属性也是可以的



图二效果:



<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.yechaoa.materialdesign.activity.TabViewPagerScrollActivity">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="@string/app_name"
            app:toolbarId="@+id/toolbar">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/app_name"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/header"
                app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </android.support.design.widget.CollapsingToolbarLayout>
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            app:tabIndicatorColor="@color/red"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/black"
            app:tabTextColor="@color/gray"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>



github:https://github.com/yechaoa/MaterialDesign

目录
相关文章
Element UI之el-tabs的样式修改字体颜色、下划线、选中/未选中
Element UI之el-tabs的样式修改字体颜色、下划线、选中/未选中
4501 0
|
安全 C++
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
2262 0
|
存储 弹性计算 网络协议
阿里云服务器ECS实例规格族是什么?不同规格CPU型号、处理器主频及网络性能参数均不同
阿里云ECS实例规格族是指具有不同性能特点和适用场景的实例类型集合。不同规格族如计算型c9i、通用算力型u1、经济型e等,在CPU型号、主频、网络性能、云盘IOPS等方面存在差异。即使CPU和内存配置相同,性能参数和价格也各不相同,适用于不同业务需求。
1020 144
|
12月前
|
供应链 物联网 BI
RFID仓库物料快速盘点新技术
RFID仓库物料盘点技术通过电子标签与读写设备实现非接触、批量识别,大幅提升盘点效率与准确率。相比传统人工方式,其具备高效、低误差、可批量操作等优势,广泛应用于仓储物流等领域,是智能化库存管理的核心解决方案。(238字)
|
自然语言处理 物联网 算法框架/工具
开源大语言模型(LLM)汇总
随着ChatGPT的火爆,越来越多人希望在本地运行一个大语言模型。为此我维护了这个开源大语言模型汇总,跟踪每天不发的大语言模型和精调语言模型。
4348 0
开源大语言模型(LLM)汇总
|
数据采集 机器学习/深度学习 运维
2025年设备管理系统盘点:如何借助智能运维减少非计划停机与维护成本?
在制造业数字化转型中,设备管理面临非计划停机与高维护成本挑战。本文基于2025年调研,解析树根科技根云平台在IIoT接入、预测性维护、知识管理等方面的领先实践,助力企业实现高效智能运维。
608 0
|
人工智能 Python
2025自学编程实操指南第一课面向AI编程
2025自学编程实操指南第一课面向AI编程,第一个实践案例:贪吃蛇游戏
Vue2滑动输入条(Slider)
这是一个基于 Vue3 的滑动输入条(Slider)组件,提供了丰富的自定义选项,如最小值、最大值、初始值、宽度、禁用状态及双滑块模式等。用户可通过拖动滑块或点击输入条调整数值。代码示例展示了如何创建组件及在页面中引入使用。包含单滑块与双滑块模式的效果图。
778 2
Vue2滑动输入条(Slider)
|
Java Linux
终于有人把内网centos安装libreoffice步骤说清楚了
终于有人把内网centos安装libreoffice步骤说清楚了
2862 2
|
安全 Java 数据库
Spring Security 权限管理详解与案例
Spring Security 是 Spring 框架中用于提供认证和访问控制的模块。它保护了成千上万的应用程序,使其免受未经授权的访问。本文将详细介绍 Spring Security 的权限管理功能,并通过一个实际案例来展示其用法。
2064 1