美文网首页
Constraintlayout便查手册

Constraintlayout便查手册

作者: Anancncn | 来源:发表于2018-06-15 11:20 被阅读0次

Constraintlayout便查手册

摘取自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

当位置约束目标的可见性为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"

偏差 : 用下面参数, 默认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 有这么几个参数可以使用

layout_constraintWidth_min and layout_constraintHeight_min : will set the minimum size for this dimension(设置最小)
layout_constraintWidth_max and layout_constraintHeight_max : will set the maximum size for this dimension(设置最大)
layout_constraintWidth_percent and layout_constraintHeight_percent : will set the size of this dimension as a percentage of the parent(设置父布局的 %)

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(百分比设置)

如果想使用百分比, 需要设置一下内容

  • The dimension should be set to MATCH_CONSTRAINT (0dp)(尺寸设置为0dp或者 match_constaraint)
  • The default should be set to percent app:layout_constraintWidth_default="percent" or app:layout_constraintHeight_default="percent" (默认值写百分比)
  • Then set the layout_constraintWidth_percent or layout_constraintHeight_percent attributes to a value between 0 and 1 (这两个参数 0-1 之间)
Ratio

参数可以设置为浮点值(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"/>
<!--设置 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 权重

当 宽高 MATCH_CONSTRAINT(0dp)的时候, 用下面的方法设置权重

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)(指定布局左侧或顶部的固定距离)
  • specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)(从布局的右侧或底部指定固定距离)
  • specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)(指定布局的宽度或高度的百分比)
  • android:orientation 方向

相关文章

网友评论

      本文标题:Constraintlayout便查手册

      本文链接:https://www.haomeiwen.com/subject/atbpeftx.html