Constraintlayout便查手冊
Relative positioning (相對定位)
[圖片上傳失敗...(image-1013da-1529032552415)]
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB"
app:layout_constraintLeft_toRightOf="@+id/buttonA" />
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
普通的 margin 依然適用
android:layout_marginStart
android:layout_marginEnd
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
Margins when connected to a GONE widget
當(dāng)位置約束目標(biāo)的可見性為View.GONE時答姥,還可以使用以下屬性指示要使用的不同邊距值:
layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom
Centering positioning and bias(居中定位)
<android.support.constraint.ConstraintLayout ...>
<Button android:id="@+id/button" ...
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent/>
</>
<!-- 除非 Button 與父布局一樣大, 否則會居中 -->
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
偏差 : 用下面參數(shù), 默認(rèn)0.5--為居中
layout_constraintHorizontal_bias
layout_constraintVertical_bias
Circular positioning (Added in 1.1)
layout_constraintCircle : references another widget id(引用另一個小部件ID)
layout_constraintCircleRadius : the distance to the other widget center(到其他小部件中心的距離"xxdp")
layout_constraintCircleAngle : which angle the widget should be at (in degrees, from 0 to 360) (角度)
Visibility behavior
Dimensions constraints(尺寸限制)
Minimum dimensions on ConstraintLayout
Those minimum and maximum dimensions will be used by ConstraintLayout when its dimensions are set to WRAP_CONTENT.
是不是 要使用 min or max
android:minWidth -- set the minimum width for the layout
android:minHeight -- set the minimum height for the layout
android:maxWidth -- set the maximum width for the layout
android:maxHeight -- set the maximum height for the layout
Widgets dimension constraints
android:layout_width and android:layout_height attributes in 3 different ways:
width 和 height 有下面三種
- Using a specific dimension (such 123dp)
- Using WRAP_CONTENT, which will ask the widget to compute its own size
- Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
MATCH_PARENT 不再適用于ConstraintLayout
WRAP_CONTENT : enforcing constraints (Added in 1.1)
If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute:
使用 WRAP_CONTENT 但仍然想使用約束 ????
app:layout_constrainedWidth=”true|false”
app:layout_constrainedHeight=”true|false”
MATCH_CONSTRAINT dimensions (Added in 1.1)
MATCH_CONSTRAINT 有這么幾個參數(shù)可以使用
layout_constraintWidth_min and layout_constraintHeight_min : will set the minimum size for this dimension(設(shè)置最小)
layout_constraintWidth_max and layout_constraintHeight_max : will set the maximum size for this dimension(設(shè)置最大)
layout_constraintWidth_percent and layout_constraintHeight_percent : will set the size of this dimension as a percentage of the parent(設(shè)置父布局的 %)
Min and Max
The value indicated for min and max can be either a dimension in Dp, or "wrap", which will use the same value as what WRAP_CONTENT would do.
min和max指示的值可以是Dp中的一個維度,也可以是“wrap”揭糕,它將使用與WRAP_CONTENT相同的值竞端。
Percent dimension(百分比設(shè)置)
如果想使用百分比, 需要設(shè)置一下內(nèi)容
- The dimension should be set to MATCH_CONSTRAINT (0dp)(尺寸設(shè)置為0dp或者 match_constaraint)
- The default should be set to percent app:layout_constraintWidth_default="percent" or app:layout_constraintHeight_default="percent" (默認(rèn)值寫百分比)
- Then set the
layout_constraintWidth_percent
orlayout_constraintHeight_percent
attributes to a value between 0 and 1 (這兩個參數(shù) 0-1 之間)
Ratio
參數(shù)可以設(shè)置為浮點(diǎn)值(0.5)或者"width:height"(1:1)
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />
<!-- 注意0dp -->
app:layout_constraintDimensionRatio
<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"/>
<!--設(shè)置 W 或者 H 來代表約束邊-->
Chains 鏈
ChainStyle
layout_constraintHorizontal_chainStyle
or layout_constraintVertical_chainStyle
When setting the attribute layout_constraintHorizontal_chainStyle or layout_constraintVertical_chainStyle on the first element of a chain, the behavior of the chain will change according to the specified style (default is CHAIN_SPREAD).
- sprread(default) -- average parent <-> view && view<-> view
- spread_inside -- average view <-> view , adjoin parent <-> view
- packed -- average parent <->view , adjoin view <-> view
- width or height is 0dp -- adjoin 0dp`s view
weight 權(quán)重
當(dāng) 寬高 MATCH_CONSTRAINT(0dp)的時候, 用下面的方法設(shè)置權(quán)重
layout_constraintHorizontal_weight and layout_constraintVertical_weight
android.support.constraint.Guideline
A Guideline can be either horizontal or vertical
- specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)(指定布局左側(cè)或頂部的固定距離)
- specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)(從布局的右側(cè)或底部指定固定距離)
- specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)(指定布局的寬度或高度的百分比)
- android:orientation 方向