Como alinhar e centralizar objetos verticalmente no layout de restrição? É possível alinhar verticalmente ou horizontalmente, mas eu não encontrei uma maneira de centralizar ao mesmo tempo além de restringir as visualizações entre duas linhas de grade.
Centro de alinhamento vertical:
Parece que a centralização é um grande problema com o layout de restrição, que me força a voltar ao layout relativo para "centerInParent", "centerVertical" e "centerHorizontal".
Eu gostaria de criar o layout em caixa em vermelho usando o layout de restrição:
Infelizmente, a única maneira que encontrei sem usar duas linhas de grade é com Relative e LinearLayouts aninhados (que o Constraint Layout supostamente resolveria esse cenário exato!).
Layout usando Layout Relativo e Linear:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintTop_toBottomOf="@id/user_points"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<LinearLayout
android:id="@+id/stat_1_layout"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/divider_1"
android:orientation="vertical">
<TextView
android:id="@+id/stat_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="10"
android:textSize="16dp"
android:textColor="@color/textSecondaryDark"
android:maxLines="1"/>
<TextView
android:id="@+id/stat_detail_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Streak"
android:textSize="8sp"
android:textColor="@color/textSecondary"
android:maxLines="1"/>
</LinearLayout>
<View
android:id="@+id/divider_1"
android:layout_width="1dp"
android:layout_height="38dp"
android:layout_toLeftOf="@+id/stat_2_layout"
android:background="@drawable/linedivider"/>
<LinearLayout
android:id="@+id/stat_2_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:id="@+id/stat_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="243"
android:textSize="16dp"
android:textColor="@color/textSecondaryDark"
android:maxLines="1"/>
<TextView
android:id="@+id/stat_detail_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Calories Burned"
android:textSize="8sp"
android:textColor="@color/textSecondary"
android:maxLines="1"/>
</LinearLayout>
<View
android:id="@+id/divider_2"
android:layout_width="1dp"
android:layout_height="38dp"
android:layout_toRightOf="@+id/stat_2_layout"
android:background="@drawable/linedivider"/>
<LinearLayout
android:id="@+id/stat_3_layout"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_toRightOf="@+id/divider_2"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/stat_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="3200"
android:textSize="16dp"
android:textColor="@color/textSecondaryDark"
android:maxLines="1"/>
<TextView
android:id="@+id/stat_detail_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Steps"
android:textSize="8sp"
android:textColor="@color/textSecondary"
android:maxLines="1"/>
</LinearLayout>
</RelativeLayout>
Respostas:
É possível definir a visualização alinhada ao centro como uma âncora para outras visualizações. No exemplo abaixo, "@ + id / stat_2" centralizado horizontalmente no pai e serve como uma âncora para outras visualizações neste layout.
<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/stat_1" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:gravity="center" android:maxLines="1" android:text="10" android:textColor="#777" android:textSize="22sp" app:layout_constraintTop_toTopOf="@+id/stat_2" app:layout_constraintEnd_toStartOf="@+id/divider_1" /> <TextView android:id="@+id/stat_detail_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Streak" android:textColor="#777" android:textSize="12sp" app:layout_constraintTop_toBottomOf="@+id/stat_1" app:layout_constraintStart_toStartOf="@+id/stat_1" app:layout_constraintEnd_toEndOf="@+id/stat_1" /> <View android:id="@+id/divider_1" android:layout_width="1dp" android:layout_height="0dp" android:layout_marginEnd="16dp" android:background="#ccc" app:layout_constraintTop_toTopOf="@+id/stat_2" app:layout_constraintEnd_toStartOf="@+id/stat_2" app:layout_constraintBottom_toBottomOf="@+id/stat_detail_2" /> <TextView android:id="@+id/stat_2" android:layout_width="80dp" android:layout_height="wrap_content" android:gravity="center" android:maxLines="1" android:text="243" android:textColor="#777" android:textSize="22sp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <TextView android:id="@+id/stat_detail_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:text="Calories Burned" android:textColor="#777" android:textSize="12sp" app:layout_constraintTop_toBottomOf="@+id/stat_2" app:layout_constraintStart_toStartOf="@+id/stat_2" app:layout_constraintEnd_toEndOf="@+id/stat_2" /> <View android:id="@+id/divider_2" android:layout_width="1dp" android:layout_height="0dp" android:layout_marginStart="16dp" android:background="#ccc" app:layout_constraintBottom_toBottomOf="@+id/stat_detail_2" app:layout_constraintStart_toEndOf="@+id/stat_2" app:layout_constraintTop_toTopOf="@+id/stat_2" /> <TextView android:id="@+id/stat_3" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:gravity="center" android:maxLines="1" android:text="3200" android:textColor="#777" android:textSize="22sp" app:layout_constraintTop_toTopOf="@+id/stat_2" app:layout_constraintStart_toEndOf="@+id/divider_2" /> <TextView android:id="@+id/stat_detail_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:text="Steps" android:textColor="#777" android:textSize="12sp" app:layout_constraintTop_toBottomOf="@+id/stat_3" app:layout_constraintStart_toStartOf="@+id/stat_3" app:layout_constraintEnd_toEndOf="@+id/stat_3" /> </android.support.constraint.ConstraintLayout>
Veja como funciona no menor smartphone (3,7 480x800 Nexus One) em comparação ao maior smartphone (5,5 1440x2560 Pixel XL)
fonte
Se você tiver um
ConstraintLayout
com algum tamanho e um filhoView
com algum tamanho menor, você pode obter a centralização restringindo as duas bordas da criança às mesmas duas bordas do pai. Ou seja, você pode escrever:app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"
ou
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
Como a visualização é menor, essas restrições são impossíveis. Mas
ConstraintLayout
fará o melhor que puder e cada restrição "puxará" a visão da criança igualmente, centralizando-a assim.Esse conceito funciona com qualquer visualização de destino, não apenas com a principal.
Atualizar
Abaixo está o XML que alcança a IU desejada sem aninhamento de visualizações e sem
Guideline
s (embora as diretrizes não sejam inerentemente más).<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="wrap_content" android:background="#eee"> <TextView android:id="@+id/title1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:gravity="center" android:textColor="#777" android:textSize="22sp" android:text="10" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/divider1" app:layout_constraintBottom_toBottomOf="parent"/> <TextView android:id="@+id/label1" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textColor="#777" android:textSize="12sp" android:text="Streak" app:layout_constraintTop_toBottomOf="@+id/title1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/divider1"/> <View android:id="@+id/divider1" android:layout_width="1dp" android:layout_height="55dp" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:background="#ccc" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toRightOf="@+id/title1" app:layout_constraintRight_toLeftOf="@+id/title2" app:layout_constraintBottom_toBottomOf="parent"/> <TextView android:id="@+id/title2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:gravity="center" android:textColor="#777" android:textSize="22sp" android:text="243" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toRightOf="@+id/divider1" app:layout_constraintRight_toLeftOf="@+id/divider2" app:layout_constraintBottom_toBottomOf="parent"/> <TextView android:id="@+id/label2" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textColor="#777" android:textSize="12sp" android:text="Calories Burned" app:layout_constraintTop_toBottomOf="@+id/title2" app:layout_constraintLeft_toRightOf="@+id/divider1" app:layout_constraintRight_toLeftOf="@+id/divider2"/> <View android:id="@+id/divider2" android:layout_width="1dp" android:layout_height="55dp" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:background="#ccc" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toRightOf="@+id/title2" app:layout_constraintRight_toLeftOf="@+id/title3" app:layout_constraintBottom_toBottomOf="parent"/> <TextView android:id="@+id/title3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="12dp" android:gravity="center" android:textColor="#777" android:textSize="22sp" android:text="3200" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toRightOf="@+id/divider2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> <TextView android:id="@+id/label3" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:textColor="#777" android:textSize="12sp" android:text="Steps" app:layout_constraintTop_toBottomOf="@+id/title3" app:layout_constraintLeft_toRightOf="@+id/divider2" app:layout_constraintRight_toRightOf="parent"/> </android.support.constraint.ConstraintLayout>
fonte
Talvez eu não tenha entendido totalmente o problema, mas centralizar todas as visualizações em um ConstraintLayout parece muito simples. Isso é o que eu usei:
<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="wrap_content" android:layout_gravity="center">
As duas últimas linhas funcionaram!
fonte
<TextView android:id="@+id/tvName" style="@style/textViewBoldLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Welcome" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>
fonte
Você pode facilmente centralizar várias coisas criando uma cadeia. Funciona tanto vertical quanto horizontalmente
Link para a documentação oficial sobre cadeias
Edite para responder ao comentário :
<?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/first_score" android:layout_width="60dp" android:layout_height="wrap_content" android:text="10" app:layout_constraintEnd_toStartOf="@+id/second_score" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/second_score" app:layout_constraintBottom_toTopOf="@+id/subtitle" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintVertical_chainStyle="packed" android:gravity="center" /> <TextView android:id="@+id/subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Subtitle" app:layout_constraintTop_toBottomOf="@+id/first_score" app:layout_constraintBottom_toBottomOf="@+id/second_score" app:layout_constraintStart_toStartOf="@id/first_score" app:layout_constraintEnd_toEndOf="@id/first_score" /> <TextView android:id="@+id/second_score" android:layout_width="60dp" android:layout_height="120sp" android:text="243" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/thrid_score" app:layout_constraintStart_toEndOf="@id/first_score" app:layout_constraintTop_toTopOf="parent" android:gravity="center" /> <TextView android:id="@+id/thrid_score" android:layout_width="60dp" android:layout_height="wrap_content" android:text="3200" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/second_score" app:layout_constraintTop_toTopOf="@id/second_score" app:layout_constraintBottom_toBottomOf="@id/second_score" android:gravity="center" /> </android.support.constraint.ConstraintLayout>
Você tem a cadeia horizontal:
first_score <=> second_score <=> third_score
.second_score
é centralizado verticalmente. As outras pontuações são centralizadas verticalmente de acordo com ele.Você pode definitivamente criar uma corrente vertical
first_score <=> subtitle
e centralizá-la de acordo comsecond_score
fonte
Mostrando isso graficamente.
A centralização no pai é feita restringindo ambos os lados ao pai. Você pode restringir objetos adicionais fora do objeto centralizado.
Nota. Cada seta representa um atributo "app: layout_constraintXXX_toYYY =". (6 na imagem)
fonte
Eu também tinha uma exigência de algo semelhante a isso. Eu queria ter um contêiner no centro da tela e dentro do contêiner existem muitas visualizações. A seguir está o código de layout xml. Aqui, estou usando o layout de restrição aninhado para criar um contêiner no centro da tela.
<androidx.constraintlayout.widget.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" android:background="@drawable/gradient_background" tools:context=".activities.AppInfoActivity"> <ImageView android:id="@+id/ivClose" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:src="@drawable/ic_round_close_24" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="300dp" android:layout_height="300dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"> <ImageView android:id="@+id/ivAppIcon" android:layout_width="100dp" android:layout_height="100dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/dead" /> <TextView android:id="@+id/tvAppName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Birds Shooter Plane" android:textAlignment="center" android:textSize="30sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ivAppIcon" /> <TextView android:id="@+id/tvAppVersion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Version : 1.0" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvAppName" /> <TextView android:id="@+id/tvDevelopedBy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="Developed by" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvAppVersion" /> <TextView android:id="@+id/tvDevelopedName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="K Pradeep Kumar Reddy" android:textAlignment="center" android:textSize="14sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvDevelopedBy" /> <TextView android:id="@+id/tvContact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="Contact : [email protected]" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvDevelopedName" /> <TextView android:id="@+id/tvCheckForUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="@string/check_for_update" android:textAlignment="center" android:textSize="14sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvContact" /> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Aqui está a imagem do layout
Outra solução é remover o layout de restrição aninhado e adicionar o atributo constraint_vertical_bias = 0.5 ao elemento superior no centro do layout. Acho que isso é chamado de encadeamento de visões.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" android:background="@drawable/gradient_background" tools:context=".activities.AppInfoActivity"> <ImageView android:id="@+id/ivClose" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:src="@drawable/ic_round_close_24" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/ivAppIcon" android:layout_width="100dp" android:layout_height="100dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" app:srcCompat="@drawable/dead" /> <TextView android:id="@+id/tvAppName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_display_name" android:textAlignment="center" android:textSize="30sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ivAppIcon" /> <TextView android:id="@+id/tvAppVersion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/version" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvAppName" /> <TextView android:id="@+id/tvDevelopedBy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="@string/developed_by" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvAppVersion" /> <TextView android:id="@+id/tvDevelopedName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/developer_name" android:textAlignment="center" android:textSize="14sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvDevelopedBy" /> <TextView android:id="@+id/tvContact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/developer_email" android:textAlignment="center" android:textSize="12sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvDevelopedName" /> <TextView android:id="@+id/tvCheckForUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="@string/check_for_update" android:textAlignment="center" android:textSize="14sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvContact" /> </androidx.constraintlayout.widget.ConstraintLayout>
Aqui está a imagem do layout
fonte