How to center multiple Views together using ConstraintLayout?

前端 未结 5 503
野的像风
野的像风 2021-02-01 12:17

Background

Google has announced a new layout called \"ConstraintLayout\" that\'s supposed to be the ultimate layout, that could replace all of the layouts while stayin

5条回答
  •  滥情空心
    2021-02-01 12:34

    Take a look at my answer here.

    ContraintLayout contains a feature - Chains - that makes it possible to implement what you are asking:

    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

    As for your case, you'll have to pack your label and description TextViews and center them in you parent vertically:

    (make sure you use a version of ConstraintLayout that supports chains)

    
    
    
    
        
    
        
    
        
    
        
    
        
    
    

    Update 25-06-2019 (@Saeid Z):

    Now in constraint layout 1.1.3 we must use app:layout_constraintHorizontal_chainStyle="packed" instead of app:layout_constraintVertical_chainPacked="true"

提交回复
热议问题