CONSTRAINTLAYOUT – GUIDELINES, BARRIERS, CHAINS AND GROUPS

转载:https://riggaroo.co.za/constraintlayout-guidelines-barriers-chains-groups/

Since my initial blog post about ConstraintLayout, there have been a whole bunch of new features added (and lots of improvements) to ConstraintLayout. This blog post aims to cover some of the new features, namely Guidelines, Barriers, Chains and Groups.
Some of these features require using Android Studio 3.0 Beta 5 and the beta version of ConstraintLayout. In order to use the beta version of ConstraintLayout, make sure you have at least the following dependency in your app level **build.gradle **file:

1

implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

Guidelines
Guidelines are small visual helpers to design layouts with. Views can be aligned to a guideline which can simplify layouts, especially if you have the same margin values duplicated on a lot of elements.
Guidelines can be specified in dp from start or end of the screen or they can be a percentageof the width of the screen. To cycle through the different guideline modes, you can click the round icon at the top of the guideline.
To create a guideline and use it, see below:
[图片上传中。。。(1)]Guidelines in ConstraintLayout

If you are curious as to how a guideline looks in XML, this is what it looks like:

ConstraintLayout Guideline

1
2
3
4
5
6

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="20dp" />

Other views in the layout can then constrain themselves to the guideline.
Barriers
Barriers are one of my favourite features in ConstraintLayout. A barrier is an invisible view that contains reference to the views that you wish to use to form a “barrier” against. If one of the views grows, the barrier will adjust its size to the largest height or width of the referenced items. Barriers can be vertical or horizontal and can be created to the top, bottom, left or right of the referenced views. Other views can then constrain themselves to the barrier.
Below is an example of creating a barrier from two views and constraining a view to it. As you can see, adjusting the size of the TextView, the barrier adjusts its size and the constrained view moves.
[图片上传中。。。(2)]Barriers in ConstraintLayout

If you are curious as to how this is created, this is what it looks like in XML:

1
2
3
4
5
6

<android.support.constraint.Barrier
    android:id="@+id/barrier"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="right"
    app:constraint_referenced_ids="button_example,text_view_status" />

The property app:constraint_referenced_ids contains a list of view ids that will form the barrier.
Chains
Chains allow you to control the space between elements and how the elements use the space. To create a chain, select the elements that you want to form part of the chain, and then right click – “Chain” – “Create Horizontal/Vertical Chain”.
[图片上传中。。。(3)]ConstraintLayout Chains

You are then able to cycle through the different chain modes. There are four different modes: spread_inside, packed, spread and weighted.

Different Chain Modes in ConstraintLayout

The XML for creating a chain is different in that all the views have the constraints defined on them and the first item in the chain specifies the chainStyle.

1
2
3
4
5
6
7
8
9

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/button_two"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    tools:text="1" />

Groups
With groups, you can logically group together certain views. Don’t confuse this with normal ViewGroups in Android though. A group in ConstraintLayout only contains references to the view ids and not nesting the views inside a group. With a group, you can set the visibility of all views in the group, by just setting the groups visibility without needing to set every view’s visibility. This is useful for things such as error screens or loading screens where a few elements need to change their visibility at once.
To add a group – see below:
[图片上传中。。。(5)]Groups in ConstraintLayout

If you are wondering how this looks in XML, here it is:

Group ConstraintLayout Example

1
2
3
4
5

<android.support.constraint.Group
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:constraint_referenced_ids="button_load,text_view_status" />

The property app:constraint_referenced_ids contains a list of all the view ids that need to be a part of the group.
Practical Example
I wanted to try recreate the Google Play Movies detail screen only using ConstraintLayout to see how much I could build in the UI Editor. The results completely blew me away.

ConstraintLayout Android Example

For the code relating to this layout, checkout this Github repository.
There are loads more features in ConstraintLayout, so subscribe to the blog to receive updates. Have anything to add? Let me know on Twitter.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,494评论 0 23
  • 今天又发现了一个问题:我不敢上台演讲。 其实这个问题很早之前就有了,刚来这边工作时,在上家公司,经理让我介绍下自己...
    jennydeer阅读 1,553评论 0 0
  • 成倍自评 关于这首诗 人物都是真的 高考的记忆已经远去 唯一记得那位高大帅气体育生 他后来真的买了奶茶 大一还给我...
    向日葵爱呀爱太阳阅读 1,450评论 0 0
  • 只想这样静静的怀疑人生…
    Applexu996阅读 1,535评论 0 0

友情链接更多精彩内容