Group views in ConstraintLayout to treat them as a single view

前端 未结 4 2021
无人共我
无人共我 2020-12-25 10:18

I need to apply some constraints to a group of views in ConstraintLayout. I want to group these views and continue editing while the layout designer in Android studio treats

4条回答
  •  独厮守ぢ
    2020-12-25 10:55

    ConstraintLayout Chains

    Android developers recently released a new version of ConstraintLayout (1.0.2 as of today). This version contains a new major feature - Chains, which allows us to group views in ConstraintLayout.

    Chains provide group-like behavior in a single axis (horizontally or vertically).

    A set of widgets are considered a chain if they a linked together via a bi-directional connection

    Once a chain is created, there are two possibilities:

    • Spread the elements in the available space
    • A chain can also be "packed", in that case the elements are grouped together

    Currently, you need to use the following gradle dependency to use this feature (since it is an alpha):

     compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
    

    Here you may find the newest version of ConstraintLayout to use in your projects.

    Until Android Studio 2.3, Android Studio user interface designer did not support creating chains since you couldn't add bi-directional constraints in it. The solution was to create these constraints in manually XML, as mentioned by TranslucentCloud. From Android Studio 2.3 (currently only on canary channel), chains are supported in a UI editor as well (as GoRoS mentioned in comments).


    Example

    Following is an example of how to position two views together in the middle of a screen using ConstraintLayout and chains:

    
    
    
    
        
    
        

    Update (Jan, 2018) by @Mateus Gondim

    In the recent versions, you should use app:layout_constraintVertical_chainStyle="packed" instead of app:layout_constraintVertical_chainPacked="true"


提交回复
热议问题