How to center multiple Views together using ConstraintLayout?

前端 未结 5 491
野的像风
野的像风 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:29

    Set app:layout_constraintVertical_bias="0.5" to the views that need to be centered vertically, bias attribute only works if you specify the constraints for the boundaries (e.g. top and bottom for vertical bias, left and right for horizontal bias)

    An example:

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/constraintLayout">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="@id/constraintLayout"
            app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
            app:layout_constraintVertical_bias="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    Got it working in my layout here: https://github.com/hidroh/tldroid/blob/master/app/src/main/res/layout/activity_main.xml, pretty much similar layout, though I position things at 1/3rd of screen height.

    0 讨论(0)
  • 2021-02-01 12:29

    To center something vertically or horizontally, set an opposing constraint on the layout.

    Centered Vertically

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
    

    Centered Horizontally

        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
    
    0 讨论(0)
  • 2021-02-01 12:33

    EDIT: This answer had been written before chains became available. Please use chains now, see above answer: https://stackoverflow.com/a/40102296/1402641


    For now I believe your only choice would be to wrap the two text views in another layout - probably linear layout best fits this situation.

    But the team behind constraint layout said they want to introduce "virtual containers", which serve exactly this use case: you group two or more views together and set a constraint (like centring vertically) on the container. The idea is that it is not a full nested layout inside of constraint layout, but just something that constraint layout uses to position it's children - hence it should provide better performance than nesting.

    They mention it in their I/O talk (linked to the exact time). So I guess stay tuned.

    0 讨论(0)
  • 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)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="16dp"
            android:text="TextView"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintLeft_toRightOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_chainStyle="packed"/>
    
        <TextView
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="Button\nMkay"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/textView"/>
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher"/>
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher"/>
    
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:srcCompat="@mipmap/ic_launcher"/>
    </android.support.constraint.ConstraintLayout>
    

    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"

    0 讨论(0)
  • 2021-02-01 12:34

    Take a look at mine example (Center components in ConstraintLayout)

    For now you could center "image" (constrain left-top-bottom), constrain "label" top to "image" top, constrain "description" top to "label" bottom. In example below i have matched button height with two textviews to the right (which is your case). Other textviews are constrained as on the link above.

    UPDATE: Answering to your comment below:

    I have installed your app and this is only i can think of currently. Since it is a ViewHolder layout, layout_height can be set to wrap_content, or you can fix it if you'd like to. I can send you xml if you want, i dont want to flood the answer.

    Top TextView left constraint is constrained to right constraint of ImageView. Also, top TextView -> top constraint is constrained to top of container, so is right. Bottom TextView is constrained to bottom of container. TextViews in the middle are constrained to match width of top TextView and got no connection with ImageView.

    0 讨论(0)
提交回复
热议问题