继承关系:
A ConstraintLayout
is a ViewGroup
which allows you to position and size widgets in a flexible way.
ConstraintLayout是一个ViewGroup,它允许你以灵活的方式去定位和调整控件(的大小)。
Note: ConstraintLayout
is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time. This documentation will reflect those changes.
注意:你可以在API等级9的Android系统及以上将ConstraintLayout作为一个支持类库使用。像这样的,我们计划随着时间的推移去丰富它的API和能力。该文档将会反映这些变化。
There are currently various types of constraints that you can use:
如下是当前你可以使用的各种类型的约束:
- Relative positioning 相对定位
- Margins 边缘约束
- Centering positioning 中心定位
- Circular positioning 环形定位
- Visibility behavior 可见性行为
- Dimension constraints 尺寸约束
- Chains 链式约束
- Virtual Helpers objects 虚拟助手对象
- Optimizer 优化器
Note that you cannot have a circular dependency in constraints.
注意,约束中不能具有循环依赖关系。
Also see ConstraintLayout.LayoutParams
for layout attributes.
你也可以通过阅读ConstraintLayout.LayoutParams去了解(其)布局属性。
Developer Guide
开发者指南
Relative positioning
相对定位
Relative positioning is one of the basic building block of creating layouts in ConstraintLayout. Those constraints allow you to position a given widget relative to another one. You can constrain a widget on the horizontal and vertical axis:
相对定位是在约束布局中创建布局的基本构建块之一。这些约束允许您将给定的小部件相对于另一个小部件定位。可以在水平和垂直轴上约束小部件:
- Horizontal Axis: left, right, start and end sides
- 水平轴:左、右、起点和终点
- Vertical Axis: top, bottom sides and text baseline
- 垂直轴:顶部、底部和文本基线
The general concept is to constrain a given side of a widget to another side of any other widget.
一般的概念是将控件的给定边约束到任何其他窗口小部件的另一边。
For example, in order to position button B to the right of button A (Fig. 1):
例如,为了将按钮B定位到按钮A的右边(如图):
you would need to do:
你需要这样做:
<Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB" ... app:layout_constraintLeft_toRightOf="@+id/buttonA" />