我对ConstraintLayout和RelativeLayout之间的区别感到困惑。有人能告诉我它们之间的确切区别吗?


当前回答

正式来说,ConstraintLayout要快得多

在Android N版本中,ConstraintLayout类提供了与RelativeLayout类似的功能,但成本要低得多。

其他回答

以下是它们的区别/优点:

Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). A very powerful use is grouping of elements by forming a chain. This way we can form a group of views which as a whole can be placed in a desired way without adding another layer of hierarchy just to form another group of views. In addition to weights, we can apply horizontal and vertical bias which is nothing but the percentage of displacement from the centre. ( bias of 0.5 means centrally aligned. Any value less or more means corresponding movement in the respective direction ) . Another very important feature is that it respects and provides the functionality to handle the GONE views so that layouts do not break if some view is set to GONE through java code. More can be found here: https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#VisibilityBehavior Provides power of automatic constraint applying by the use of Blue print and Visual Editor tool which makes it easy to design a page.

所有这些特性都使视图层次结构变得平坦,从而提高了性能,还有助于制作响应性和动态的UI,从而更容易适应不同的屏幕尺寸和密度。

这里是快速学习的最佳地点: https://codelabs.developers.google.com/codelabs/constraint-layout/#0

相对布局和约束布局的等效属性

(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"

The only difference i've noted is that things set in a relative layout via drag and drop automatically have their dimensions relative to other elements inferred, so when you run the app what you see is what you get. However in the constraint layout even if you drag and drop an element in the design view, when you run the app things may be shifted around. This can easily be fixed by manually setting the constraints or, a more risky move being to right click the element in the component tree, selecting the constraint layout sub menu, then clicking 'infer constraints'. Hope this helps

我能得出的结论是

1)我们可以在不接触xml部分代码的情况下进行UI设计,老实说,我觉得谷歌已经复制了iOS应用程序中的UI设计,如果你熟悉iOS中的UI开发,这将是有意义的,但在相对布局中,很难在不接触xml设计的情况下设置约束。

2)其次,它具有平面视图层次结构,不像其他布局,因此性能比相对布局更好,你可能已经从其他答案中看到

3)除了相对布局之外,它还有一些额外的东西,比如圆形相对定位,我们可以将另一个视图相对于这个视图以一定的半径和一定的角度放置,这在相对布局中是做不到的

我再说一遍,使用约束布局设计UI和在iOS中设计UI是一样的,所以以后如果你在iOS上工作,你会发现使用约束布局会更容易

真正要问的问题是,是否有理由使用约束布局以外的任何布局?我相信答案可能是否定的。

对于那些坚持认为它们是针对新手程序员或类似的人,它们应该提供一些理由来证明它们不如其他任何布局。

约束布局在各个方面都更好(它们的APK大小确实花费了150k)。它们更快、更简单、更灵活,能够更好地应对变化,能够在项目消失时修复问题,能够更好地适应完全不同的屏幕类型,并且不会使用一堆嵌套的循环和长长的树形结构。你可以把任何东西放在任何地方,相对于任何地方。

它们在2016年年中有点古怪,可视化布局编辑器不够好,但它们已经到了这样的地步,如果你有一个布局,你可能会认真考虑使用约束布局,即使它做的事情与RelativeLayout相同,甚至是一个简单的线性布局。框架布局显然仍然有他们的目的。但是,在这一点上,我看不到任何其他东西。如果从这个开始,就不会再加其他东西了。