• 首页
  • 狐文
  • 狐图
  • About
狐窝
OvO
  1. 首页
  2. Android
  3. 正文

ConstraintLayout 和 Coordinator 对比

2022年10月20日 24点热度 0人点赞 0条评论

简单来说:
CoordinatorLayout 是一个超级强大的 FrameLayout。
ConstraintLayout 是一个类似于RelativeLayout 的超级强大的ViewGroup,但比RelativeLayout 更灵活。
  在摸索新技术是发现CoordinatorLayout 与 ConstraintLayout 会有冲突关系,所以就研究了一下他们之间的不兼容,被影响的方面.其实某种程度上来说是CoordinatorLayout与其他Layout布局之间的关系.

  首先说明一下:

  CoordinatorLayout: Material Design 的根布局,作为协调Material下所有控件的动画联动.所以被称为协调者布局

  ConstraintLayout: google为了将布局扁平化,减少嵌套而设计的约束布局

  它们的关系
  1.CoordinatorLayout必需做为根布局存在,才能使它的子view或者layout都有动画关联效果

  2.ConstraintLayout 不可以作为跟布局嵌套 CoordinatorLayout,否则会让全部Material都没有动画效果

  3.ConstraintLayout不可以嵌套AppBarLayout布局,一样会影响Material都没有动画效果

  4.ConstraintLayout最好嵌套在最后一层的布局里,作为为其他控件提供确定位置的布局

资料来自:https://stackoverflow.com/questions/40929686/constraintlayout-vs-coordinator-layout
https://www.cnblogs.com/guanxinjing/p/10158562.html

CoordinatorLayout is a super-powered FrameLayout.

CoordinatorLayout

CoordinatorLayout is intended for two primary use cases:

As a top-level application decor or chrome layout
As a container for a specific interaction with one or more child views
By default, if you add multiple children to a FrameLayout, they would overlap each other. A FrameLayout should be used most often to hold a single child view. The main appeal of the CoordinatorLayout is its ability to coordinate the animations and transitions of the views within it. By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the CoordinatorLayout.DefaultBehavior annotation.

Behaviors may be used to implement a variety of interactions and additional layout modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons that stick to other elements as they move and animate.

ConstraintLayout is a super-powered ViewGroup similar to a RelativeLayout, but more flexible than RelativeLayout.

ConstraintLayout

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.

ConstraintLayout can be used anywhere, you don't need any other ViewGroup like RelativeLayout, LinearLayout or FrameLayout once you start using ConstraintLayout.
There are currently various types of constraints that you can use:

Relative positioning
Margins
Centering positioning
Circular positioning
Visibility behavior
Dimension constraints
Chains
Virtual Helpers objects
Optimizer
What to implement: ConstraintLayout or CoordinatorLayout for proper material design in android ?
You may need to use both ConstraintLayout and CoordinatorLayout to build efficient UI and material animations.

A common example which uses both CoordinatorLayout and ConstraintLayout is given below for your reference.

Use Coordinatorlayout as the top-level application decor. It will usually used to layout AppBarLayout , FloatingActionButton, and the main body of your screen, say NestedScrollView. Inside the NestedScrollView use ConstraintLayout to describe the rest of the layout as a flat hierarchy.

<androidx.coordinatorlayout.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">

      <androidx.core.widget.NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

          <!-- Your scrolling content -->
          <androidx.constraintlayout.widget.ConstraintLayout
              ...>

              <!-- body of constraint layout -->

              <Button android:id="@+id/button" ...
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent/>

          </androidx.constraintlayout.widget.ConstraintLayout>
     </androidx.core.widget.NestedScrollView>

     <com.google.android.material.appbar.AppBarLayout
         android:layout_height="wrap_content"
         android:layout_width="match_parent">
      <androidx.appcompat.widget.Toolbar
             ...
             app:layout_scrollFlags="scroll|enterAlways"/>
      <com.google.android.material.tabs.TabLayout
             ...
             app:layout_scrollFlags="scroll|enterAlways"/>
      </com.google.android.material.appbar.AppBarLayout>
 </androidx.coordinatorlayout.widget.CoordinatorLayout>

What do the above snippet? here you go.

We have placed the androidx.coordinatorlayout.widget.CoordinatorLayout as the root layout. And we put androidx.core.widget.NestedScrollView and com.google.android.material.appbar.AppBarLayout as direct children.
We defined app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" attribute for androidx.core.widget.NestedScrollView. This is the key point. We defined a behavior for the NestedScrollView. That is we are telling the Coordinator layout that the NestedScrollView depends on the AppBarLayout.

Of course, Behaviors don’t do anything on their own, but CoordinatorLayout does. It act accordingly and helps to intercept touch events, window insets, measurement, layout, and nested scrolling. So here, it places the NestedScrollView below the AppBarLayout as we instructed. Cool right?
We placed the ConstraintLayout inside the NestedScrollView to make it scrollable. As we already discussed, the ConstraintLayout is used to align child views with in the bounds of the ConstraintLayout.

Can I add ConstraintLayout inside another ConstraintLayout?

Of course yes, You can use any combination to align views as per your design requirements.

Can I add CoordinatorLayout inside another CoordinatorLayout ?

That is not a usual practice. the most common use case of CoordinatorLayout is as a the top-level application decor to coordinate between other direct children. But yes, if you really want to nest the CoordinatorLayout, you can do so by creating a custom CoordinatorLayout which extends the CoordinatorLayout and implements NestedScrollingChild to pass the scroll events to the parent CoordinatorLayout.

Bonus point
You can use the powerful MotionLayout which is a subclass of ConstraintLayout for building animations. You may check this for a detailed example for custom animation using MotionLayout.

标签: android constraintlayout coordinator 和 对比
最后更新:2022年10月21日

OvO

狐狸

点赞
< 上一篇
最新 热点 随机
最新 热点 随机
EFI Shell 命令参考命令 说明 证书的SCT相关 ssl证书生成时插入OID TrueNAS SCALE docker安装使用联合文件系统mergerfs Iptables 端口(流量)转发 如何在GIT中删除第一个提交 TrueNas 虚拟机无法连接主机 dnsmasq ipset iptables 实现对流量进行分流 新版 Ubuntu 修改IP DCHP K3S 创建portainer 通过声音进行数据传输 Windows精简部署相关简易内容 ConstraintLayout 和 Coordinator 对比 Android 为View分配id wireshark解析TCP tcpdump rpcapd 相关编译方法 双网卡调整优先级 Android 禁用IPv6
EFI Shell 命令参考命令 说明
svn命令行批量删除和批量添加 Debian8 更新源配置 Win下最爱效率利器:AutoHotKey Windows DPAPI 数据加密保护接口详解 有关Dockerfile 多阶段构建原理和使用场景的分享 ESXI 安装时用到的几个命令 世界,您好! 蓝牙设备 Windows和Ubuntu 双系统配置 根据mac地址获取生产厂商 Ubuntu20如何禁用IPV6 关于 N1 旁路由的设置 生成和打上patch的方法 Android 系统上的 Xposed 框架中都有哪些值得推荐的模块? Oracle 12c 添加scott用户 PyCharm下打包py程序成exe Linux VPS主机安全 下一主题 谈谈主从分布式爬虫与对等分布式爬虫的优劣 使用Windows Kits创建PE 及精简镜像
标签聚合
http https 未分类 git 下载 编译 ssl android ipv com docker 网卡 密码 windows chrome linux

COPYRIGHT © 2020 狐窝. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS