前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ConstraintLayout2.0一篇写不完之约束到底写在哪

ConstraintLayout2.0一篇写不完之约束到底写在哪

作者头像
用户1907613
发布2021-06-17 20:23:31
7400
发布2021-06-17 20:23:31
举报
文章被收录于专栏:Android群英传Android群英传

点击上方蓝字关注我,知识会给你力量

熟读约束三百遍,不会使用也会吹。

https://developer.android.com/reference/androidx/constraintlayout/motion/widget/MotionLayout

约束到底写在哪

MotionLayout的约束有几种写法,这个问题跟茴香豆的茴有几种写法还真不太一样。

在MotionLayout中,约束可以写在三个地方。

  • 直接写在MotionLayout中:如果布局简单,那么可以直接写在MotionLayout中,这样Scene中的约束会直接继承Layout。
  • 写在Scene中:这是官方推荐的做法,不同的State对应不同的ConstraintSet。
  • 写在单独的CL中:Transition的State不仅仅可以使用ConstraintSet,也可以使用单独的ConstraintLayout布局文件。

❝默认情况下,所有的约束都来自于Layout,这也是IDE中,默认显示的Source是Layout的原因。 ❞

这几种写法各有利弊,首先,写在MotionLayout中,会被State中的布局约束覆盖,但是写在Scene中,每个State都要重复写大量的约束,写在单独的CL布局中,则无法预览,所以,成年人的世界,就是要你全都会,结合不同的使用场景,需要用不同的约束策略。

Sectioned Constraints

前面说了,在Scene的ConstraintSet中,你需要对每个发生变化的元素创建Constraint,而且一旦你创建了这个元素的Constraint,那么Layout中的这个元素的所有约束都将失效,例如你修改了width,即使height没有修改,也需要重写。

Constraint位于ConstraintSet标签内部,用于描述当前的约束行为,你可以把它当作是一个简化的ConstraintLayout,可以在其内部创建约束,但更好的做法是通过layout、motion、transform等标签来对约束进行分类,从而可以更好的理清约束和动画的关系。

所以,当你不想重写所有属性的修改时,可以使用Sectioned Constraints,它有五个实现:

  • Layout:这里面你可以修改和布局相关的状态变更,例如布局约束和尺寸
  • Transform:这里你可以修改Transform相关的状态变更,例如rotation、translationX等
  • PropertySet:这里你可以修改View的属性,例如Visibility
  • Motion:这里你可以修改跟MotionLayout相关的属性,例如pathMotionArc、TransitionEasing等
  • CustomAttribute:这里你可以修改你设置的CustomAttribute

借助它们,可以少写无关的重复属性,简化代码。

Derived Constraints

ConstraintSet可以从另一个ConstraintSet中派生,这样可以优化约束的复用,利用deriveConstraintsFrom属性,你可以指定另一个ConstraintSet的id进行引用,然后在其中修改需要覆盖的属性。

ConstraintOverride

这个标签是2.1新增的一个属性,也存在于ConstraintSet标签下,于Constraint同级。它包含了Constraint所有的属性,除了layout_constraintXX_toXXOf之类提供锚点信息的属性。利用这个属性,可以很方便的针对某些非锚点信息的变化的属性进行修改。

Constraint Tags

Constraint的标签支持两种形式。

  • 所有的ConstraintLayout+下面列出的那些+
  • 组合标签:、、、、。使用这些标签的好处是,如果不存在这些属性,则从基本布局文件中获取。如果只需要一个Motion标签,这就可以省去复制所有的布局标签。如果使用了这些标签,那么基础文件中的所有布局属性都会被覆盖。

属性列表:

属性

含义

android:id

Id of the View

[ConstraintLayout attributes]

Any attribute that is part of ContraintLayout layout is allowed

[Standard View attributes]

A collection of view attributes supported by the system (see below)

transitionEasing

define an easing curve to be used when animating from this point (e.g. curve(1.0,0,0,1.0)) or key words {standard | accelerate | decelerate | linear }

pathMotionArc

the path will move in arc (quarter eclipses) or key words {startVertical | startHorizontal | none }

transitionPathRotate

(float) rotate object relative to path taken

drawPath

draw the path the layout will animate animate

progress

call method setProgress(float) on this view (used to talk to nested ConstraintLayouts etc.)

call a set"name" method via reflection

Attributes for the ConstraintLayout e.g. layout_constraintTop_toTopOf

currently only visibility, alpha, motionProgress,layout_constraintTag.

All the view transform API such as android:rotation.

Motion Layout control commands such as transitionEasing and pathMotionArc

Layout

属性

含义

[ConstraintLayout attributes]

see for attribute

PropertySet

属性

含义

visibility

set the Visibility of the view. One of Visible, invisible or gone

alpha

setAlpha value

motionProgress

using reflection call setProgress

layout_constraintTag

a tagging string to identify the type of object

Transform

属性

含义

android:elevation

base z depth of the view.

android:rotation

rotation of the view, in degrees.

android:rotationX

rotation of the view around the x axis, in degrees.

android:rotationY

rotation of the view around the y axis, in degrees.

android:scaleX

scale of the view in the x direction

android:scaleY

scale of the view in the y direction.

android:translationX

translation in x of the view. This value is added post-layout to the left property of the view, which is set by its layout.

android:translationY

translation in y of the view. This value is added post-layout to the top property of the view, which is set by its layout

android:translationZ

translation in z of the view. This value is added to its elevation.

app:transformPivotTarget

transform target

android:transformPivotX

transform pivot x

android:transformPivotY

transform pivot y

Motion

属性

含义

transitionEasing

Defines an acceleration curve.

pathMotionArc

Says the object should move in a quarter ellipse unless the motion is vertical or horizontal

motionPathRotate

set the rotation to the path of the object + this angle.

drawPath

Debugging utility to draw the motion of the path

animateCircleAngleTo

Animate with circle

animateRelativeTo

Animate relative to target

quantizeMotionSteps

quantize animation with steps

CustomAttribute

属性

含义

attributeName

The name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)

customColorValue

The value is a color looking setMyAttr(int )

customIntegerValue

The value is an integer looking setMyAttr(int )

customFloatValue

The value is a float looking setMyAttr(float )

customStringValue

The value is a String looking setMyAttr(String )

customDimension

The value is a dimension looking setMyAttr(float )

customBoolean

The value is true or false looking setMyAttr(boolean )

向大家推荐下我的网站 https://xuyisheng.top/ 点击原文一键直达

专注 Android-Kotlin-Flutter 欢迎大家访问

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 群英传 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 约束到底写在哪
  • Sectioned Constraints
  • Derived Constraints
  • ConstraintOverride
  • Constraint Tags
    • Layout
      • PropertySet
        • Transform
          • Motion
            • CustomAttribute
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档