Enzio
Enzio

Reputation: 889

layout_gravity doesn't work when parent layout is ConstraintLayout

I know that layout_gravity = "center" would center the current View or Layout in it's parent layout.

In the code example I have given, Both the first TextView and the LinearLayout cannot have layout_gravity as a property when their parent is ConstraintLayout.

However, the second TextView and the second LinearLayout can have layout_gravity property when their parent is a LinearLayout.

Why is that so?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="heey"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"></LinearLayout>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Upvotes: 2

Views: 7492

Answers (1)

Nitesh Verma
Nitesh Verma

Reputation: 2500

ConstraintLayout doesn't support layout_gravity. you should use

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

to centre the layout.

Upvotes: 9

Related Questions