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

图片

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

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

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

推断约束图片

您可能拥有包含属性的小部件:

  tools:layout_editor_absoluteX="someValue" tools:layout_editor_absoluteY="someValue" 

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

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

 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 

您可以查看ConstraintLayout的文档和使用ConstraintLayout 构建响应式UI以获取更多详细信息

编辑:

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