<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ConstraintLayout2.0一篇寫不完之約束到底寫在哪

          共 5082字,需瀏覽 11分鐘

           ·

          2021-06-03 20:29

          點擊上方藍字關注我,知識會給你力量

          熟讀約束三百遍,不會使用也會吹。

          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布局中,則無法預覽,所以,成年人的世界,就是要你全都會,結(jié)合不同的使用場景,需要用不同的約束策略。

          Sectioned Constraints

          前面說了,在Scene的ConstraintSet中,你需要對每個發(fā)生變化的元素創(chuàng)建Constraint,而且一旦你創(chuàng)建了這個元素的Constraint,那么Layout中的這個元素的所有約束都將失效,例如你修改了width,即使height沒有修改,也需要重寫。

          Constraint位于ConstraintSet標簽內(nèi)部,用于描述當前的約束行為,你可以把它當作是一個簡化的ConstraintLayout,可以在其內(nèi)部創(chuàng)建約束,但更好的做法是通過layout、motion、transform等標簽來對約束進行分類,從而可以更好的理清約束和動畫的關系。

          所以,當你不想重寫所有屬性的修改時,可以使用Sectioned Constraints,它有五個實現(xiàn):

          • Layout:這里面你可以修改和布局相關的狀態(tài)變更,例如布局約束和尺寸
          • Transform:這里你可以修改Transform相關的狀態(tài)變更,例如rotation、translationX等
          • PropertySet:這里你可以修改View的屬性,例如Visibility
          • Motion:這里你可以修改跟MotionLayout相關的屬性,例如pathMotionArc、TransitionEasing等
          • CustomAttribute:這里你可以修改你設置的CustomAttribute

          借助它們,可以少寫無關的重復屬性,簡化代碼。

          Derived Constraints

          ConstraintSet可以從另一個ConstraintSet中派生,這樣可以優(yōu)化約束的復用,利用deriveConstraintsFrom屬性,你可以指定另一個ConstraintSet的id進行引用,然后在其中修改需要覆蓋的屬性。

          ConstraintOverride

          這個標簽是2.1新增的一個屬性,也存在于ConstraintSet標簽下,于Constraint同級。它包含了Constraint所有的屬性,除了layout_constraintXX_toXXOf之類提供錨點信息的屬性。利用這個屬性,可以很方便的針對某些非錨點信息的變化的屬性進行修改。

          Constraint Tags

          Constraint的標簽支持兩種形式。

          • 所有的ConstraintLayout+下面列出的那些+

          • 組合標簽:、、。使用這些標簽的好處是,如果不存在這些屬性,則從基本布局文件中獲取。如果只需要一個Motion標簽,這就可以省去復制所有的布局標簽。如果使用了這些標簽,那么基礎文件中的所有布局屬性都會被覆蓋。

          屬性列表:

          屬性含義
          android:idId 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)
          transitionEasingdefine 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 }
          pathMotionArcthe path will move in arc (quarter eclipses) or key words {startVertical | startHorizontal | none }
          transitionPathRotate(float) rotate object relative to path taken
          drawPathdraw the path the layout will animate animate
          progresscall 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

          屬性含義
          visibilityset the Visibility of the view. One of Visible, invisible or gone
          alphasetAlpha value
          motionProgressusing reflection call setProgress
          layout_constraintTaga tagging string to identify the type of object

          Transform

          屬性含義
          android:elevationbase z depth of the view.
          android:rotationrotation of the view, in degrees.
          android:rotationXrotation of the view around the x axis, in degrees.
          android:rotationYrotation of the view around the y axis, in degrees.
          android:scaleXscale of the view in the x direction
          android:scaleYscale of the view in the y direction.
          android:translationXtranslation 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:translationYtranslation 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:translationZtranslation in z of the view. This value is added to its elevation.
          app:transformPivotTargettransform target
          android:transformPivotXtransform pivot x
          android:transformPivotYtransform pivot y

          Motion

          屬性含義
          transitionEasingDefines an acceleration curve.
          pathMotionArcSays the object should move in a quarter ellipse unless the motion is vertical or horizontal
          motionPathRotateset the rotation to the path of the object + this angle.
          drawPathDebugging utility to draw the motion of the path
          animateCircleAngleToAnimate with circle
          animateRelativeToAnimate relative to target
          quantizeMotionStepsquantize animation with steps

          CustomAttribute

          屬性含義
          attributeNameThe name of the attribute. Case sensitive. ( MyAttr will look for method setMyAttr(...)
          customColorValueThe value is a color looking setMyAttr(int )
          customIntegerValueThe value is an integer looking setMyAttr(int )
          customFloatValueThe value is a float looking setMyAttr(float )
          customStringValueThe value is a String looking setMyAttr(String )
          customDimensionThe value is a dimension looking setMyAttr(float )
          customBooleanThe value is true or false looking setMyAttr(boolean )

          向大家推薦下我的網(wǎng)站 https://xuyisheng.top/  點擊原文一鍵直達

          專注 Android-Kotlin-Flutter 歡迎大家訪問



          往期推薦


          本文原創(chuàng)公眾號:群英傳,授權轉(zhuǎn)載請聯(lián)系微信(Tomcat_xu),授權后,請在原創(chuàng)發(fā)表24小時后轉(zhuǎn)載。
          < END >
          作者:徐宜生

          更文不易,點個“三連”支持一下??

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  在线观看免费黄色视频 | 国产一级电影在线播放 | 日韩少妇内射 | 国产操九千 | 精品久久一区二区三区四区五区 |