【问题标题】:why android studio show error of "Missing constraints in constraintlayout"?为什么android studio显示“约束布局中缺少约束”的错误?
【发布时间】:2018-05-31 15:42:04
【问题描述】:

picture

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

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

【问题讨论】:

  • 你能把你的布局文件贴在这里吗?
  • 您是否复制了说明其工作原理的文本而不是提出问题?
  • 我认为你应该从一个关于约束布局如何工作的快速教程开始youtube.com/watch?v=2FOFd77o7eg
  • To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections - 好吧,你可以如何解决它。
  • 我有同样的问题:要复制它,在向导上创建一个带有空活动的新项目,并在 cotraintlayout 中添加一个文本视图。 (AS 3.1.3,这里是新手 android 程序员)

标签: java android android-layout android-studio android-fragments


【解决方案1】:

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

【讨论】:

  • 当我们点击推断约束按钮时,会在activity_main.xml中添加约束。 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"
【解决方案2】:

您可能有带有属性的小部件:

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

tools 命名空间仅在开发时使用,在安装 apk 时将被删除,因此您所有的布局可能会出现在 TOP-LEFT 的位置上。查看Tools Attributes Reference

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

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

您可以通过ConstraintLayoutBuild a Responsive UI with ConstraintLayout的文档了解更多详情

编辑:

从您发布的picture 中,我尝试使用以下代码添加适当的约束,使 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>

【讨论】:

  • 你能用你尝试过的更新一下吗,这样会更容易帮助你
  • thanx Firoz Memon
【解决方案3】:

“ConstraintsLayout 中缺少约束”。由于未定义的约束而发生此错误。要修复此错误,请单击小部件(例如文本、按钮等),然后单击活动屏幕上方的推断约束。希望这解决了您的问题

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多