Align a group of views containing a chain in ConstraintLayout

I have three views in ConstraintLayout, and I want to align them as follows:

enter image description here

Right now, species B and C form a vertical chain, and A is centered relative to this chain. But how do I center the entire group in the parent? Please note that View C may be GONE.

+13
source share
2 answers

Here is a visual answer without cutting layouts.

enter image description here

measures

  1. Chain and package B and C vertically
  2. Chain and package A and C horizontally
  3. B C

XML

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="69dp"
        android:layout_height="67dp"
        android:background="#fb0000"
        android:gravity="center"
        android:text="A"
        android:textColor="#000000"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="154dp"
        android:layout_height="73dp"
        android:background="#2000ff"
        android:gravity="center"
        android:text="B"
        android:textColor="#ffffff"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="@+id/textView3"
        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="187dp"
        android:layout_height="61dp"
        android:background="#f1a500"
        android:gravity="center"
        android:text="C"
        android:textColor="#000000"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>

. , ConstraintLayout.

+13

- , ConstraintLayout, ConstraintLayout :

<android.support.constraint.ConstraintLayout 
    android:id="@+id/outerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/innerLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        [A, B, and C views here...]

    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

ViewGroup, LinearLayout, gravity.

, , , , .

+1

Source: https://habr.com/ru/post/1679843/


All Articles