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

guykilcj  于 2022-11-13  发布在  Android
关注(0)|答案(5)|浏览(486)

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

vcirk6k6

vcirk6k61#

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

ogsagwnx

ogsagwnx2#

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

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

tools命名空间只在开发时使用,并将在安装apk时删除,因此所有您的布局可能会出现在左上角的位置一个在另一个之上。查看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处于Center位置:

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

5hcedyr03#

ConstraintsLayout”中缺少约束。此错误是由于未定义的约束引起的。要修复此错误,请单击小部件(ex text、按钮等),然后单击活动屏幕上方的推断约束。希望这样可以解决您的查询

ycggw6v2

ycggw6v24#

enter image description here
首先点击组件树中线性布局,然后点击活动屏幕中眼睛符号右侧推断约束

r1zk6ea1

r1zk6ea15#

如果您确实为视图指定了有效的约束,但Android Studio仍然向您推送了一个“幻影”MissingConstraint错误,请尝试以下操作:
1.清理项目(* 生成〉清理项目
1.关闭项目(
文件〉关闭项目 *)

*移除项目的.idea/目录

  • 如果将文件从.idea/签入源代码管理,请回滚它们
    *打开您的项目
    重建您的项目( 重建〉重建项目 *)

这个神圣的净化仪式帮助我摆脱了这个“幻影”错误,以及过去许多其他怪异的错误,所以我希望它也能帮助你。

相关问题