解决:This view is not constrained vertically: at runtime it will jump to the top unless you...

This view is not constrained vertically

项目中使用 androidx.constraintlayout.widget.ConstraintLayout 时Android Studio中的布局的子控件可能会提示如下错误:

This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint
This view is not constrained horizontally: at runtime it will jump to the left unless you add a horizontal constraint
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints

例如布局文件是:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        app:layout_constraintStart_toStartOf="parent"/>
    <!-- This view is not constrained horizontally: at runtime it will jump to the left unless you add a horizontal constraint -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        app:layout_constraintTop_toTopOf="parent"/>
    <!-- This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这是因为 ConstraintLayout 中子控件的约束条件不完整,约束完整即可:水平和垂直的约束都要有。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

作者:ITmob
来源:ITmob.cn
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×