To summarize the advantages of constraint layouts:
From the developer’s use: constraint layout is a powerful upgrade version of the RelativeLayout, RelativeLayout can realize ConstraintLayout can also realize, RelativeLayout ConstraintLayout is not possible can be achieved.
In terms of performance, interpret Layout basically does not have the multi-level nesting of ViewGroup. We know that the drawing of View starts from the top layer and completes the measure, layout and draw of the whole interface in the form of traversal. ConstraintLayout has some performance advantages in rendering complex layouts.
A Visual interface editing
B can reduce the interface level nesting, as flat as possible
C controls can be positioned relative to each other and have the advantages of a RelativeLayout and a LinearLayout
Limitations:
Like RelativeLayout and LinearLayout, the size of the control can’t be adjusted with different resolutions. Instead, it can be adjusted with relatively comfortable relationships between controls.
After version 1.0, the control size can be adjusted in percentage based on the parent control.
2, the introduction of
Build. Gradle:
Implementation 'androidx. Constraintlayout: constraintlayout: 2.0.1'Copy the code
ConstraintLayout can then be introduced into the layout file.
3. Relative layout properties
The basic Constraint property, which can be set to a position within the parent, aligns layout_constraintLeft_toLeftOf // to the left relative to other controls
Layout_constraintRight_toRightOf // align to the right
Layout_constraintTop_toTopOf // Aligned to the top
Layout_constraintBottom_toBottomOf // Aligned to the bottom
//start=left end=right
Layout_constraintRight_toLeftOf // Adjacent, to the left
Layout_constraintLeft_toRightOf // Adjacent, on its right
Layout_constraintTop_toBottomOf // Adjacent, at the bottom
Layout_constraintBottom_toTopOf // Adjacent, at its top
Layout_constraintBaseline_toBaselineOf // Baseline alignment
4. Percentage attributes
You can set layout_constrainWidth_percent to the parent control
ayout_constrainHeight_percent
5. Chain style
-
Spread – Elements will be expanded (default style)
-
Weighted chain – In Spread mode, if some widgets are set to MATCH_CONSTRAINT, they split the available space
-
Spread_inside – similar, but the endpoints of the chain will not expand
-
Packed – The elements of the chain will be packed together. App :layout_constraintHorizontal_chainStyle=”spread”
6, Bias
With bias, the constraint can be biased to a certain side. The default value is 0.5 and has the following attributes:
-
Layout_constraintHorizontal_bias (0 to the left and 1 to the right)
-
Layout_constraintVertical_bias (0 uppermost 1 lowermost)
You can use this property to achieve a percentage distribution of the relative layout.