美文网首页
ConstraintLayout的属性介绍

ConstraintLayout的属性介绍

作者: 快乐的小2缺 | 来源:发表于2018-07-06 14:08 被阅读0次

以constraint-layout:1.1.2为例:

1、普通约束

layout_constraintLeft_toLeftOf:控件的左侧与目标控件的左侧对齐

layout_constraintLeft_toRightOf:控件的左侧与目标控件的右侧对齐

layout_constraintRight_toRightOf:控件的右侧与目标控件的右侧对齐

layout_constraintRight_toLeftOf:控件的右侧与目标控件的左侧对齐

layout_constraintTop_toTopOf:控件的顶部与目标控件的顶部对齐

layout_constraintTop_toBottomOf:控件的顶部与目标控件的底部对齐

layout_constraintBottom_toBottomOf:控件的底部与目标控件的底部对齐

2、百分比约束

layout_constraintDimensionRatio:控件宽高比约束,前提是至少将宽高中一项设置为0dp。为了约束一个特定的边,基于另一个边的尺寸,可以预先附件W或者H

app:layout_constraintDimensionRatio="H,16:9"

约束高,宽高比为16:9

layout_constraintWidth_percent:控件宽度与父控件宽度的比例

app:layout_constraintWidth_percent="0.5"

控件宽度为父控件一半

layout_constraintHeight_percent:控件高度与父控件高度的比例。

layout_constraintHorizontal_bias:控件水平偏好比例,0.5为中间,小于0.5为偏向左侧,大于0.5为偏向右侧。

app:layout_constraintHorizontal_bias="0.5"

0.5为水平居中;0为水平居左;1为水平居右

layout_constraintVertical_bias:控件垂直偏好比例。

3、 goneMargin(隐藏边界)

layout_goneMarginStart:目标控件设置为gone时,起始边界。当布局从左往右时,等同于layout_goneMarginLeft,当布局从右往左时等同于layout_goneMarginRight。

layout_goneMarginEnd:目标控件设置为gone时,结束边界。当布局从左往右时,等同于layout_goneMarginRight,当布局从右往左时等同于layout_goneMarginLeft。

layout_goneMarginLeft:目标控件设置为gone时,左边边界。

layout_goneMarginRight:目标控件设置为gone时,右边边界。

layout_goneMarginTop:目标控件设置为gone时,顶部边界。

layout_goneMarginBottom:目标控件设置为gone时,底部边界。

4、 Chains 链状约束

Chain Style是使多个控件像链条一样显示。

要想chain style生效,必须设置当前链方向的边为wrap_content,比如上面的水平链,宽设为wrap_content。其次控件要相互引用,比如A的右边依赖B的左边,B的左边依赖A的右边,都是设置。 如:水平方向的Chain Style.

layout_constraintHorizontal_chainStyle:水平链。属性值包括:spread(均匀分布)、spread_inside(均匀分布,但是边上的控件不均匀分布)、packed(控件紧挨在一起,可以通过bias属性设置偏移量)

layout_constraintVertical_chainStyle:垂直链。属性同上。

layout_constraintHorizontal_weight:跟线性布局的weight差不多,layout_constraintHorizontal_weight需要设置width=0dp,控件的大小根据设置的weight比例进行设置。

layout_constraintVertical_weight :跟线性布局的weight差不多,layout_constraintVertical_weight 需要设置height=0dp,控件的大小根据设置的weight比例进行设置。

5、 Guideline

Guideline是一条隐形的线,这条线可作为准线,根据此准线设置其他控件的位置。

GuideLine支持的属性:

layout_constraintGuide_begin:距离left或top的大小 

layout_constraintGuide_end:距离right或bottom的大小 

layout_constraintGuide_percent:float类型。距离left或top的大小的比例。 

android:orientation=”vertical” ,那么guideline距离左侧的距离为父布局宽度的一半。

相关文章

网友评论

      本文标题:ConstraintLayout的属性介绍

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