How to center the child view but keep aspect ratio in ConstraintLayout

127 views Asked by At

I am using ConstraintLayout that contains child view. I want this child view centered but keeping aspect ratio.

Layout (landscape) variant:

<?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:id="@+id/previewParent"
    tools:context=".PeakerActivity">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="0px"
        android:layout_height="0px"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintDimensionRatio="16:9"
        android:background="@color/black"
        android:gravity="center"
        android:id="@+id/flexbox2">


    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.constraintlayout.widget.ConstraintLayout>

Adding android:gravity="center" has no effect, child view is still not centered.

Result

Edit: Added image, as you see, left purple side is greater than right side.

2

There are 2 answers

0
Aks4125 On BEST ANSWER

Try this, with app:layout_constraintHorizontal_bias layout attribute by keeping it half (0.5).

<?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:id="@+id/previewParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingDefaultResource">

<androidx.appcompat.widget.LinearLayoutCompat
    android:id="@+id/flexbox2"
    android:layout_width="0px"
    android:layout_height="0px"
    android:background="@color/black"
    android:gravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintDimensionRatio="16:9"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


</androidx.appcompat.widget.LinearLayoutCompat>

</androidx.constraintlayout.widget.ConstraintLayout>

Bias

The default when encountering such opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes:

0
王 能 On

Based on the image you provided, it seems that your layout extends below the navigation bar or does not extend above the status bar. To troubleshoot this issue, consider adding a TextView at both the bottom and top of your ConstraintLayout. This will help you verify if the positioning is correct.