Constraintlayout便查手册

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 方向
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,331评论 0 10
  • 简书MarkDown支持有点问题,请跳转到我博客查看该篇博客:http://weiyf.cn/2017/03/10...
    卫裕发阅读 4,688评论 0 4
  • ❖对不起,我的父母 给你们打电话的时候太少 回家的次数更是屈指可数 策划这份工作太忙太累太晚 下班后就一句多余的话...
    策划圈群主艾伦阅读 273评论 0 0
  • 感觉家里亲戚做面食很快,这郭家一伙说话也很快。得对付得了机器。 龙在教师岗位见人见多,反应比较快,说话描述好玩的事...
    治青阅读 143评论 0 0
  • 准备期:资源+方案 时间:活动开始前2个月 1、资源确认 ①市场渠道(如应用宝、360手机卫士、百度手机助手、PP...
    _Sya阅读 266评论 0 0