我对ConstraintLayout和RelativeLayout之间的区别感到困惑。有人能告诉我它们之间的确切区别吗?
当前回答
由@davidpbr ConstraintLayout性能报告
我做了两个类似的7个子布局,每个都有一个父ConstraintLayout和RelativeLayout。 基于Android Studio方法跟踪工具,它似乎ConstraintLayout花更多的时间在onMeasure和执行额外的工作在onfinishinflation。
使用的库(support-v4, appcompat-v7…):
com.android.support.constraint: constraint-layout: 1.0.0-alpha1
设备/Android版本转载于: 三星Galaxy S6 (SM-G920A;对不起,没有Nexus atm)。Android 5.0.2
快速方法跟踪比较:
示例Github回购:https://github.com/OnlyInAmerica/ConstraintLayoutPerf
其他回答
真正要问的问题是,是否有理由使用约束布局以外的任何布局?我相信答案可能是否定的。
对于那些坚持认为它们是针对新手程序员或类似的人,它们应该提供一些理由来证明它们不如其他任何布局。
约束布局在各个方面都更好(它们的APK大小确实花费了150k)。它们更快、更简单、更灵活,能够更好地应对变化,能够在项目消失时修复问题,能够更好地适应完全不同的屏幕类型,并且不会使用一堆嵌套的循环和长长的树形结构。你可以把任何东西放在任何地方,相对于任何地方。
它们在2016年年中有点古怪,可视化布局编辑器不够好,但它们已经到了这样的地步,如果你有一个布局,你可能会认真考虑使用约束布局,即使它做的事情与RelativeLayout相同,甚至是一个简单的线性布局。框架布局显然仍然有他们的目的。但是,在这一点上,我看不到任何其他东西。如果从这个开始,就不会再加其他东西了。
由@davidpbr ConstraintLayout性能报告
我做了两个类似的7个子布局,每个都有一个父ConstraintLayout和RelativeLayout。 基于Android Studio方法跟踪工具,它似乎ConstraintLayout花更多的时间在onMeasure和执行额外的工作在onfinishinflation。
使用的库(support-v4, appcompat-v7…):
com.android.support.constraint: constraint-layout: 1.0.0-alpha1
设备/Android版本转载于: 三星Galaxy S6 (SM-G920A;对不起,没有Nexus atm)。Android 5.0.2
快速方法跟踪比较:
示例Github回购:https://github.com/OnlyInAmerica/ConstraintLayoutPerf
相对布局和约束布局的等效属性
(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)约束布局等效:
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)约束布局等效:
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)约束布局等效:
app:layout_constraintRight_toRightOf="@id/view"
(14)相对布局:
android:layout_alignTop="@id/view"
(14)约束布局等效:
app:layout_constraintTop_toTopOf="@id/view"
(15)相对布局:
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"
(17)相对布局:
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"
一个很大的区别是,即使视图消失了,ConstraintLayout仍然尊重约束。所以它不会破坏布局如果你有一个链你想让一个视图消失在中间。
ConstraintLayout的目的是通过对每个视图应用一些规则来避免嵌套,从而优化和平抑布局的视图层次结构。
规则类似于RelativeLayout,例如将底边设置为其他视图的底部。
app:layout_constraintBottom_toBottomOf="@+id/view1"
与RelativeLayout不同的是,ConstraintLayout提供了一个偏置值,用于相对于手柄(用红圈标记)的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 -->
Baseline句柄(圆角的长管道,在圆句柄的下面)用于将视图的内容与另一个视图引用对齐。
正方形句柄(在视图的每个角落)用于在dps中调整视图的大小。
这完全是基于个人观点和我对ConstraintLayout的印象
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。