ConstraintLayout
和RelativeLayout
有什么区别?
ConstraintLayout
和RelativeLayout
有什么区别?
ConstraintLayout
的意图是通过对每个视图应用一些规则来优化和扁平化布局的视图层次结构,以避免嵌套。
这些规则与 RelativeLayout
类似,例如将底部边缘设置为另一个视图的底部。
app:layout_constraintBottom_toBottomOf="@+id/view1"
与RelativeLayout
不同,ConstraintLayout
提供了一个bias值,该值用于以相对于手柄(用红色圆圈标记)的0%和100%水平和垂直偏移来定位视图。这些百分比(和分数)提供了在不同屏幕密度和大小之间无缝定位视图的功能。app:layout_constraintHorizontal_bias="0.33" <!-- from 0.0 to 1.0 -->
app:layout_constraintVertical_bias="0.53" <!-- from 0.0 to 1.0 -->
基准线柄(一个带有圆角的长管,位于圆形柄下方)用于将视图内容与另一个视图参考对齐。
每个角落上的正方形柄用于以dp为单位调整视图大小。
ConstraintLayout
的印象。相对布局和约束布局等效属性
(1) 相对布局:
android:layout_centerInParent="true"
(1) 约束布局的等效方式:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
(2) 相对布局:
android:layout_centerHorizontal="true"
(2) 约束布局的等效方式:
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
(3) 相对布局:
android:layout_centerVertical="true"
(3) 约束布局等效:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
(4) 相对布局:
android:layout_alignParentLeft="true"
(4) 约束布局相当于:
app:layout_constraintLeft_toLeftOf="parent"
(5) 相对布局:
android:layout_alignParentStart="true"
(5) 约束布局等效:
app:layout_constraintStart_toStartOf="parent"
(6) 相对布局:
android:layout_alignParentRight="true"
(6) 约束布局等效:
app:layout_constraintRight_toRightOf="parent"
(7) 相对布局:android:layout_alignParentEnd="true"
(7) 约束布局等效:
app:layout_constraintEnd_toEndOf="parent"
(8) 相对布局:
android:layout_alignParentTop="true"
(8) 约束布局的等效替代:
app:layout_constraintTop_toTopOf="parent"
(9) 相对布局:android:layout_alignParentBottom="true"
(9) Constraint Layout相当于:
app:layout_constraintBottom_toBottomOf="parent"
(10) 相对布局:
android:layout_alignStart="@id/view"
(10) 约束布局等效:
app:layout_constraintStart_toStartOf="@id/view"
(11) 相对布局:android:layout_alignLeft="@id/view"
(11) Constraint Layout 相当于:
app:layout_constraintLeft_toLeftOf="@id/view"
(12) 相对布局:
android:layout_alignEnd="@id/view"
(12) 约束布局等效:
app:layout_constraintEnd_toEndOf="@id/view"
(13) 相对布局:
android:layout_alignRight="@id/view"
(13) Constraint Layout 相当于:
app:layout_constraintRight_toRightOf="@id/view"
(14) 相对布局:
android:layout_alignTop="@id/view"
(14) Constraint Layout 等效布局:
app:layout_constraintTop_toTopOf="@id/view"
相对布局:android:layout_alignBaseline="@id/view"
(15) 约束布局等效:
app:layout_constraintBaseline_toBaselineOf="@id/view"
(16) 相对布局:
android:layout_alignBottom="@id/view"
(16) 约束布局的等效实现:
app:layout_constraintBottom_toBottomOf="@id/view"
相对布局:android:layout_toStartOf="@id/view"
(17) 约束布局等效方法:
app:layout_constraintEnd_toStartOf="@id/view"
(18) 相对布局:
android:layout_toLeftOf="@id/view"
(18) 约束布局的等效形式:
app:layout_constraintRight_toLeftOf="@id/view"
(19) 相对布局:
android:layout_toEndOf="@id/view"
(19) 约束布局等效方法:
app:layout_constraintStart_toEndOf="@id/view"
(20) 相对布局:
android:layout_toRightOf="@id/view"
(20) 约束布局的等效实现:
app:layout_constraintLeft_toRightOf="@id/view"
(21) 相对布局:android:layout_above="@id/view"
(21) 约束布局的等效实现:
app:layout_constraintBottom_toTopOf="@id/view"
(22) 相对布局:
android:layout_below="@id/view"
(22) 约束布局的等效实现:
app:layout_constraintTop_toBottomOf="@id/view"
@davidpbr 报告了 ConstraintLayout
的性能问题。
我创建了两个相似的有7个子视图的布局,一个是父布局为ConstraintLayout
,另一个是RelativeLayout
。
根据Android Studio中的方法跟踪工具显示,ConstraintLayout
在onMeasure中花费更多时间,并在onFinishInflate
中执行额外的工作。
使用的库(support-v4
,appcompat-v7
…):
com.android.support.constraint:constraint-layout:1.0.0-alpha1
复现的设备/Android版本: 三星Galaxy S6 (SM-G920A。抱歉,目前没有Nexus)。 Android 5.0.2
快速方法跟踪比较:
示例Github代码库:https://github.com/OnlyInAmerica/ConstraintLayoutPerf
约束布局具有相对布局和线性布局的双重功能:设置视图的相对位置(如相对布局),还可以为动态 UI 设置权重(这在线性布局中是唯一可能的)。
一个非常强大的用途是通过形成链来分组元素。这样,我们可以形成一组视图,这些视图作为一个整体可以以所需的方式放置,而不必添加另一层层次结构来形成另一组视图。
除了权重之外,我们还可以应用水平和垂直偏差,这只是距离中心的百分比。(偏差为0.5表示居中对齐。任何较小或较大的值都意味着在相应方向上进行相应的移动)。
另一个非常重要的功能是,它尊重并提供处理 GONE 视图的功能,以便如果通过 Java 代码将某个视图设置为 GONE,则布局不会中断。更多信息请参见: https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#VisibilityBehavior
通过使用蓝图和可视化编辑器工具提供自动约束应用的功能,这使得设计页面变得容易。
所有这些功能都导致视图层次结构的扁平化,从而提高了性能,并有助于创建响应式和动态 UI,可以更轻松地适应不同的屏幕大小和密度。
这是快速学习的最佳场所: https://codelabs.developers.google.com/codelabs/constraint-layout/#0
官方声明中,ConstraintLayout
的性能比RelativeLayout要快得多。
在Android的N版本中,
ConstraintLayout
类提供了与RelativeLayout
类似的功能,但成本显著降低。
这是否值得将旧视图从RelativeLayout或LinearLayout转换为其上的重构?
一如既往:这取决于具体情况。
除非当前布局层次存在性能问题或您想要进行重大更改,否则我不会重构任何内容。尽管我最近没有进行过测量,但在最近的版本中,我没有发现任何性能问题。因此,我认为您可以放心使用它。但是-正如我所说的-不要只是为了迁移而迁移。只有在需要以及从中获益时才这样做。 不过,对于新布局,我几乎总是使用ConstraintLayout。与之前的设计相比,它要好得多。
RealtiveLayout
、LinearLayout
、GridLayout
等)来获取他们想要的视图层次结构。 - CopsOnRoad