Project configuration
Note: You can start using API Level 9
ext{
constraint = ' 1.0.2 '
}
implementation com.android.support.constraint:constraint-layout:$ Ext.constraint "
The available constraint types relative positioning margin centered positioning annular positioning visible sexual behavior dimension limit chain Virtual Assistant
Note: You cannot recycle dependencies in the above constraints
Relative positioning
Relative positioning is one of the basic building blocks for creating layouts in Constraintlayout. These constraints allow you to position a given widget relative to another widget.
You can constrain a widget horizontal axis on both horizontal and vertical axes: left, right vertical axis: top, bottom, and text baselines
List of available constraints 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
Margins
If the side margins are set, they are applied to the appropriate constraint (if any) (Figure 3), and the margin is executed as space between the target and the source side. The list of available constraints (only positive or equal to zero, and requires a dimension) Android:layout_marginstart Android:layout_marginend Android:layout_marginleft Android:layout_margintop android:layout_marginright android:layout_marginbottom margin when connected to gone widget Layout_ Gonemarginstart layout_gonemarginend layout_gonemarginleft layout_gonemargintop layout_goneMarginRight layout_ Gonemarginbottom
Center positioning and bias bias centering
<android.support.constraint.constraintlayout ...>
<button android:id= "@+id/button"
... app:layout_constraintleft_toleftof= "Parent"
app:layout_constraintright_torightof= "parent/>
</ >
Bias
<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/>
</>
Annular positioning (added in 1.1)
A widget center that restricts the center of a widget relative to another widget at a certain angle and distance. This allows you to place a widget on a circle layout_constraintcircle: referencing another widget ID Layout_constraintcircleradius: The distance to the center of other widgets
Layout_constraintcircleangle: The angle (degrees, from 0 to 360) that the widget should be in
<button android:id= "@+id/buttona" .../> <button android:id= "
@+id/buttonb"
... App:layout_constraintcircle= "@+id/buttona"
app:layout_constraintcircleradius= "100DP"
app:layout_ Constraintcircleangle= "/>"
Visible Sex
Constraintlayout the specific handling of widgets that have been marked as View.gone.
Gone as usual, widgets will not be displayed, nor are they part of the layout itself.
But in terms of layout calculations, the Gone widget is still part of it, important difference: for the layout process, their dimensions will be treated as 0 (basically, they will be resolved to a point) if they have restrictions on other widgets, they will still be respected, but any margin will be equal to zero
Note: The margin that is used will be the margin defined by B when connecting to a. In some cases, this may not be what you want (for example, A is only 16DP to the left with 100dp,b to a, mark A to disappear, and B has 16dp on the left). For this reason, when connecting to a widget that is marked as disappearing, you can specify an alternate vanishing margin (gonemarginleft), as described in
Dimension limits can define the minimum and maximum dimensions for the constraintlayout itself: Android:minwidth set the minimum width of the layout android:minheight set the minimum height of the layout android:maxwidth Set the maximum width of the layout android:maxheight set the maximum height of the layout
Note: constraintlayout Use these minimum and maximum dimensions when the dimension is set wrap_content. Widget size Limits:
The size of the widget can be specified by setting the Android:layout_width and Android:layout_height properties in 3 different ways: using a specific dimension (or literal value 123DP or dimension reference) to use the WRAP_ CONTENT, it will require the widget to compute its own size using 0DP, equivalent to "Match_constraint" (the maximum position full of constraints, unlike match_parent full parent layout)
Note: Match_parent is not recommended for widget Constraintlayout included in a. Wrap_content: Force constraint (added in 1.1)
If a dimension is set to Wrap_content, they will be treated as text dimensions in versions prior to 1.1-which means that the constraints do not limit the dimensions that are generated. Although this is usually sufficient (and faster), in some cases you may need to use wrap_content but still enforce constraints to limit the dimensions of the build. In this case, you can add a corresponding attribute: app:layout_constrainedwidth= "True|false" app:layout_constrainedheight= "True|false" MATCH_ Constraint dimensions (added in 1.1)
When the dimension setting is Match_constraint, the default behavior is to have the generated size occupy all available space. There are several additional modifiers available: Layout_constraintwidth_min and Layout_constraintheight_min: The minimum size for this dimension will be set Layout_constraintwidth_ Max and Layout_constraintheight_max: The maximum dimensions for this dimension will be set layout_constraintwidth_percent and Layout_constraintheight_ Percent: the minimum and maximum percentage of the size of this dimension is set to the parent
The values indicated by Min and Max can be either a dimension in a DP or a dimension in "wrap", which uses the same values as the wrap_content. Percent dimension
To use percentages, you need to set the following: The dimension should be set to Match_constraint (0DP) The default value should be set to a percentage app:layout_constraintwidth_default= "percent" or app:layout _constraintheight_default= "percent"
(Note: This is required in 1.1-beta1 and 1.1-BETA2, but if the percent attribute is defined, it is not required in the following version and then the Layout_constraintwidth_percent or Layout_ The Constraintheight_percent property is set to a value scale between 0 and 1
You can also define one dimension of a widget as a scale for another dimension. To do this, you need to set at least one constraint dimension to 0DP (that is, match_constraint) and set the property layout_constraintdimensionratio to a given ratio.
<button android:layout_width= "wrap_content"
android:layout_height= "0DP"
app:layout_ constraintdimensionratio= "1:1"/>
Sets the height of the button to be the same as its width.
If all two dimensions are set to Match_constraint (0DP), you can also use ratios. In this case, the system sets the maximum size for all constraints and maintains the specified aspect ratio. To constrain a particular edge based on another dimension, you can prefix the W, "or H," to limit the width or height, respectively. For example, if a dimension is constrained by two targets (for example, the width is 0dp and centered on the father), you can indicate which side should be constrained by using the letter W (used to limit width) or h (for limiting height) in front of the ratio, separated by commas:
<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 "/>
Sets the height of the button at a scale of 16:9, and the width of the button matches the constraint to the parent item.
Chain
A chain provides similar groups of behavior in a single axis (horizontal or vertical). The other axis can be independently constrained. Create a chain
If a set of widgets are joined together through a two-way connection, they are treated as chains.
Chain Head
The chain is controlled by the property set on the first element of the chain (the "head" of the chain):
The head is the leftmost part of the horizontal chain and the topmost part of the vertical chain. Chain margin
If the part has margins in the link, the margins will be taken into account, and the margin will be deducted from the chain in the remaining space on the chain.
When you set the first element of a property layout_constrainthorizontal_chainstyle or Layout_constraintvertical_chainstyle chain, The behavior of the chain changes according to the specified style (default is Chain_spread). Chain_spread-element will be dispersed (default style) weighted chained chain_spread mode, if some widgets are set to Match_constraint, they will split free space chain_spread_inside-similar, But the end of the chain is not dispersed chain_packed-the elements of the chain will be packaged together. A child's horizontal or vertical bias attribute affects the position-weighted chain of packaged elements
The default behavior of a chain is to evenly distribute the elements in the free space. If one or more elements are using match_constraint, they will use the available white space (evenly distributed among them). Attributes Layout_constrainthorizontal_weight and Layout_constraintvertical_weight control how space-utilized elements are allocated between match_constraint. For example, in a chain containing two elements, the first element is weighted 2, the second weight is 1, and the first element occupies twice times the space of the second element. Match_constraint
Virtual Helper Object
Guideline
Secondary objects are not displayed on the device (they are marked as View.gone) and are used for layout purposes only.
The guideline can be horizontal or vertical: the width of the vertical guide is zero, the height of the constraintlayout parent is zero level and the width of the constraintlayout parents
There are three different ways to locate a guideline: Specify a fixed distance (layout_constraintguide_begin) at the left or top of the layout to specify a constant range (Layout_constraintguide_end) from the right or bottom of the layout Specify the percentage of the width or height of the layout (layout_constraintguide_percent)