Android Studio Layout Editor ConstraintLayout: pack vs chain

試著忘記壹切 提交于 2020-01-21 04:37:08

问题


In the Android Studio Layout Editor I see the words "pack" and "chain" with respect to a ConstraintLayout. I know they both have something to do with multiple views being connected to each other in a line, but their difference is not clear to me.

What is the difference between "pack" and "chain"?


回答1:


Short answer

A Chain is a group of views that are bound together in a vertical or horizontal line. Pack means that the views should be kept very closely together, ie, touch each other (excluding any padding).

Pack

Pack means that the views are packed tightly together. Imagine that you are packing your clothes tightly together in a suitcase before going on a trip.

Points to note:

  • You can pack views horizontally or vertically.
  • Packing doesn't in itself doesn't constrain the views to each other.

    • If they are not already chained, then packing moves their absolute position in the Layout Editor so that they are adjacent to each other.
    • If they are chained, then setting the Chain mode to packed binds them closely together.

      app:layout_constraintHorizontal_chainStyle="packed"
      

Chain

A chain occurs when two adjacent views both have constraints to each other. Imagine an iron link chain.

The documentation shows this well.

This chain can continue where every link (view) in the chain has a two-way link to its neighbors.

Chain mode/style

As I briefly mentioned above, there are different modes or styles for a chain. They are shown below (image from docs).

  1. Spread (default)
  2. Spread inside
  3. Weighted
  4. Packed

All of these are chained. Only the bottom one is packed.



来源:https://stackoverflow.com/questions/46639504/android-studio-layout-editor-constraintlayout-pack-vs-chain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!