4

ConstraintLayout 相关属性

 3 years ago
source link: https://wuzhaohui026.github.io/2018/01/18/ConstraintLayout%20%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ConstraintLayout 相关属性

2018-01-18

| Android

这篇主要主要 ConstraintLayout 的相关属性,加上年纪大了,记性不好,方便自己以后忘了好看看的。

以下属性都引用 id 另一个部件,或者 parent (将引用父容器,即 ConstraintLayout):
假设有 A、B 两个控件。

  • layout_constraintLeft_toLeftOf:A 控件与 B 控件左侧对齐;
  • layout_constraintLeft_toRightOf:A 控件的左侧在 B 控件的右侧;
  • layout_constraintRight_toLeftOf:A 控件的右侧在 B 控件的左侧;
  • layout_constraintRight_toRightOf:A 控件与 B 控件右侧对齐;
  • layout_constraintTop_toTopOf:A 控件与 B 控件顶部对齐;
  • layout_constraintTop_toBottomOf:A 控件的顶部与 B 控件底部对齐;
  • layout_constraintBottom_toTopOf:A 控件的底部与 B 控件顶部对齐;
  • layout_constraintBottom_toBottomOf:A 控件与 B 控件底部对齐;
  • layout_constraintBaseline_toBaselineOf:A 控件与 B 控件基线对齐;(看下图,自己找Baseline)
  • layout_constraintStart_toEndOf:A 控件的开始处在 B 控件的结束处;
  • layout_constraintStart_toStartOf:A 控件与 B 控件开始处对齐;
  • layout_constraintEnd_toStartOf:A 控件的结束处在 B 控件的开始处;
  • layout_constraintEnd_toEndOf:A 控件与 B 控件结束处对齐;
    相对定位

如果侧边距被设置,它们将被应用到相应的约束(如果存在的话),强制边缘作为目标和源端之间的间隙。

  • android:layout_marginStart:A 控件的开始处与 B 控件的边距;
  • android:layout_marginEnd:A 控件的结束处与 B 控件的边距;
  • android:layout_marginLeft:A 控件的左侧与 B 控件的边距;
  • android:layout_marginTop:A 控件的顶部与 B 控件的边距;
  • android:layout_marginRight:A 控件的右侧与 B 控件的边距;
  • android:layout_marginBottom:A 控件的底部与 B 控件的边距
    相对定位的边距

请注意,边距只能是正值或等于零,并且需要一个尺寸。

当一个指定约束目标的可见性为View.GONE,还可以使用以下属性指示要使用的不同边距值。这个我是这样理解的,A 与 B 两个控件,A 与 B 约束,然后,B 控件设置 View.Gone 属性,此时 B 控件的位置消失,A 保留与 B 控件之间的边距。(如果错了欢迎指正啊)

  • layout_goneMarginStart:A 控件与 B 控件开始处的边距;
  • layout_goneMarginEnd:A 控件与 B 控件结束处的边距;
  • layout_goneMarginLeft:A 控件与 B 控件左侧的边距;
  • layout_goneMarginTop:A 控件与 B 控件顶部的边距;
  • layout_goneMarginRight:A 控件与 B 控件右侧的边距;
  • layout_goneMarginBottom:A 控件与 B 控件底部的边距;

Bias(偏差的意思吧)

  • layout_constraintHorizontal_bias:水平方向的偏差;
  • layout_constraintVertical_bias:垂直方向的偏差;

使用百分比

  • layout_constraintCircle:引用其他控件的 id;
  • layout_constraintCircleRadius:到其他小部件中心的距离;
  • layout_constraintCircleAngle:小部件应该在哪个角度(度数,从0到360);

ConstraintLayout上的最小/最大尺寸

  • android:minWidth:设置布局的最小宽度;
  • android:minHeight:设置布局的最小高度;
  • android:maxWidth:设置布局的最大宽度;
  • android:maxHeight:设置布局的最大高度;

注意,给控件设置尺寸的时候,可以这样来做

  1. 使用一个特定的大小;
  2. 使用WRAP_CONTENT,这将要求小部件计算自己的大小;
  3. 使用0dp,相当于”MATCH_CONSTRAINT”

MATCH_CONSTRAINT尺寸

当维度设置为时MATCH_CONSTRAINT,默认行为是使得到的大小占用所有可用空间。

  • layout_constraintWidth_min 和 layout_constraintHeight_min:将设置这个尺寸的最小尺寸
  • layout_constraintWidth_max 和 layout_constraintHeight_max:将为此尺寸设置最大尺寸
  • layout_constraintWidth_percent 和 layout_constraintHeight_percent:将这个尺寸的大小设置为父级的百分比

百分比尺寸

要使用百分比,您需要设置以下内容:

  • 尺寸应该设置为 MATCH_CONSTRAIN(0dp);
  • 默认值应设置为百分比 app:layout_constraintWidth_default=”percent” 或 app:layout_constraintHeight_default=”percent”
    (注意:这在1.1-beta1和1.1-beta2中是必须的,但如果定义percent属性,则在以下版本中不需要);
  • 然后将 layout_constraintWidth_percent 或 layout_constraintHeight_percent 属性设置为介于0和1之间的值。

Ratio

控件之间的占位比。
需要设置一个约束尺寸0dp(即MATCH_CONSTRAINT),并将该属性 layout_constraintDimensionRatio 设置为给定的比率。

1
2
3
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1" />

参考:
官方文档中关于ConstraintLayout的部分


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK