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

新手上路,请多包涵

图片

这个视图没有约束,它只有设计时的位置,所以它会跳到 (0,0) 除非你添加约束

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

原文由 pawas kr. singh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 955
2 个回答

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

推断约束图片

原文由 IoT_Newbie 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

要解决此问题:您应该使用 Layout Constraints,例如:

 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 位于中心位置:

 <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>

原文由 Firoz Memon 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题