当父布局为 ConstraintLayout 时,layout_gravity 不起作用

Enz*_*zio 2 android android-layout

我知道 layout_gravity = "center" 会将当前视图或布局置于其父布局中。

在我给出的代码示例中,当它们的父项是 ConstraintLayout 时,第一个 TextView 和 LinearLayout 都不能将 layout_gravity 作为属性。

但是,当第二个 TextView 和第二个 LinearLayout 的父级是 LinearLayout 时,它们可以具有 layout_gravity 属性。

为什么呢?

<?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>
Run Code Online (Sandbox Code Playgroud)

Nit*_*rma 9

ConstraintLayout不支持layout_gravity。你应该使用

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Run Code Online (Sandbox Code Playgroud)

使布局居中。