为什么android studio显示错误"在constraintlayout中缺少约束"?

paw*_*ngh -1 java android android-layout android-fragments android-studio

图片

此视图不受约束,它只有设计时间位置,因此除非添加约束,否则它将跳转到(0,0)

布局编辑器允许您将小部件放在画布上的任何位置,并使用设计时属性(例如layout_editor_absolute X)记录当前位置.这些属性不会在运行时应用,因此如果您在设备上推送布局,窗口小部件可能出现在与编辑器中显示的位置不同的位置.要解决此问题,请通过从边连接拖动来确保窗口小部件具有水平和垂直约束

小智 21

解决这个问题非常简单.只需单击小部件(例如按钮或文本框等),然后单击"推断约束"按钮.您可以在附图或Youtube链接中看到:https://www.youtube.com/watch?v = uOur51u5Nk0

推断约束图片


Fir*_*mon 5

您可能拥有具有以下属性的小部件:

    tools:layout_editor_absoluteX="someValue"
    tools:layout_editor_absoluteY="someValue"
Run Code Online (Sandbox Code Playgroud)

tools命名空间仅在开发时使用,在安装 apk 时将被删除,因此所有布局可能会出现在左上角位置。查看工具属性参考

要解决此问题:您应该使用布局约束,例如:

layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf
Run Code Online (Sandbox Code Playgroud)

您可以浏览ConstraintLayout文档和使用 ConstraintLayout 构建响应式 UI了解更多详细信息

编辑:

从您发布的图片中,我尝试使用以下代码添加适当的约束,使 TextView 位于中心位置:

<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="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)