Dowemo

constraintlayout, which is a co TRAI not layout, launched by google I o in 2016. From support force, it becomes a mainstream layout style, completely replacing other layouts, reducing layout levels, and optimizing rendering performance. The namespace is app:, which comes from the constraintlayout package. The latest ve ion is. 0. 1.
dependencies {


 compile 'com.android.support.constraint:constraint-layout:1.0.1'


}

1. Relative position.
This relative position is somewhat similar to the relativelayout layout toLeftOf, alignParentLeft, and so on.
A constraintlayout total supports the relative position properties here:
Layout_constraintleft_toleftof
Layout_constraintleft_torightof
Layout_constraintright_toleftof
Layout_constraintright_torightof
Layout_constrainttop_totopof
Layout_constrainttop_tobottomof
Layout_constraintbottom_totopof
Layout_constraintbottom_tobottomof
Layout_constraintbaseline_tobaselineof
Layout_constraintstart_toendof
Layout_constraintstart_tostartof
Layout_constraintend_tostartof
Layout_constraintend_toendof
For the fi & t property, layout_constraintleft_toleftof ="@ + id/id_first", which represents the alignment of the left edge of the current view and the view of the id first. In fact, this property is translated into chinese: The left side of the current view is aligned with the left of the reference view

<Button android:id="@+id/buttonA".. ./>


 <Button android:id="@+id/buttonB".. .


 app:layout_constraintLeft_toRightOf="@+id/buttonA"/>

The effect is as follows:
这里写图片描述

Of course, these properties are also supported to align the left and right sides of the parent layout.

<Button android:id="@+id/buttonB".. .


 app:layout_constraintLeft_toLeftOf="parent"/>

In this way, buttonB is attached to the left side of the parent layout. Like realitelayout alignParentLeft.
2. Margin property.
This property is similar to the margin property of other layouts. The properties supported are as follows:
Android: layout_marginstart
Android: layout_marginend
Android: layout_marginleft
Android: layout_margintop
Android: layout_marginright
Android: layout_marginbottom
Here's a description of the picture.
这里写图片描述
Note: if a is closer to the left of the parent layout, b distance a 100dp, a is set to gone, then b distance is the left 100dp of the parent layout.

Gonemargin property

The layout is more interesting, or take the above picture to make an example, assuming that we've such a requirement: Assuming a is set to gone, then b needs to distance the left 200dp of the parent layout. At this point, the gonemargin property is useful, as long as you set the layout gonemarginleft = 200dp of b. In this way, a isn't gone, b distance a 100dp, a is gone, b distance from the left to the left of the parent layout.
The total supported properties are as follows:
Layout_gonemarginstart
Layout_gonemarginend
Layout_gonemarginleft
Layout_gonemargintop
Layout_gonemarginright
Layout_gonemarginbottom

Centering and bias, center, or scale offset.
Let's say we want to set up a control center? It's easy to take advantage of the properties described above.

<android.support.constraint.ConstraintLayout.. .>


 <Button android:id="@+id/button".. .


 app:layout_constraintHorizontal_bias="0.3"


 app:layout_constraintLeft_toLeftOf="parent"


 app:layout_constraintRight_toRightOf="parent/>

The properties supported by the bias are as follows:
Layout_constrainthorizontal_bias
Layout_constraintvertical_bias

And constraintlayout itself supports minheight, minwidht attributes, nothing good.

Note constraintlayout doesn't support the match pare not property, with 0dp instead of the co & TRAI not attribute with setting properties to achieve the effect of match pare not.

Ratio to set aspect ratio.

Property: app: layout_constraintdimensionratio =""
Current control is wide or high, you can use the ration property to determine additional height or width based on determined width or height.

<Button android:layout_width="wrap_content"


 android:layout_height="0dp"


 app:layout_constraintDimensionRatio="1:2"/>

Current button width has been determined, height = 2 x width based on set ratio
Layout constraintdimensionratio accepts the value of two float types that represent the width: Height.
A ration property also supports this setting:

<Button android:layout_width="0dp"


 android:layout_height="0dp"


 app:layout_constraintDimensionRatio="H,16:9"


 app:layout_constraintBottom_toBottomOf="parent"


 app:layout_constraintTop_toTopOf="parent"/>

Assume the scale is set to a: B, if the front is h, no matter which side is determined, the other side of the size is to determine the edge of b a times. If the front is w, no matter which side is determined, the size of the other side is determined by a b times.
3. Chains chain structure.

Look at the following:
这里写图片描述

Here's a description of the picture.
Chain Style
To take the chain style to take effect, you must set the edge of the current chain to wrap content, such as the horizontal chain above, and width to wrap. And that's the control to reference each other, such as the right side of a to b, on the left side of b, on the left side of b, and is set to the right.
Chain style is set on the fi & t control
Here's a description of the picture.

这里写图片描述
It's set on head.

There are two properties:
Layout_constrainthorizontal_chainstyle
Layout_constraintvertical_chainstyle
There are three supported values:
Chain_spread: evenly distributed controls.
Chain_spread_inside, similar, but the control on the edge doesn't distribute evenly.
Chain_packed: controls are immediately together. You can also set offset through the bias property.
Depending on the meaning of the three attributes, the effect is as follows:
Here's a description of the picture.
这里写图片描述
Weighted chains:
App: layout_constrainthorizontal_weight
App: layout_constraintvertical_weight
As with the weight of the linear layout, layout constrainthorizontal weight needs to set width = 0dp, and the size of the control is set based on the weight scale of the set.

4. Guideline.
A guideline is an invisible line that can be used as a guide to set the location of other controls based on this alignment

<android.support.constraint.ConstraintLayout


 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.support.constraint.Guideline


 android:layout_width="wrap_content"


 android:layout_height="wrap_content"


 android:id="@+id/guideline"


 app:layout_constraintGuide_begin="100dp"


 android:orientation="vertical"/>



 <Button


 android:text="Button"


 android:layout_width="wrap_content"


 android:layout_height="wrap_content"


 android:id="@+id/button"


 app:layout_constraintLeft_toLeftOf="@+id/guideline"


 android:layout_marginTop="16dp"


 app:layout_constraintTop_toTopOf="parent"/>



</android.support.constraint.ConstraintLayout>

Above: the guideline control is a hidden vertical line, distance from the left to 100dp, and the button is also distance from the left to the left.

Properties supported by guideline:

Layout_constraintguide_begin: size left or top
Layout_constraintguide_end: distance from right or bottom
Layout_constraintguide_percent: the scale of the float type, distance left or top.
Layout_constraintguide_percent ="0. 5"android: orientation ="vertical", then guideline distance from the left to half the width of the left.

Reference http://blog. Csdn. net/u013706904/article/details/52957946.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs