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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47866062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:42:08  来源:igfitidea点击:

why android studio show error of "Missing constraints in constraintlayout"?

javaandroidandroid-layoutandroid-studioandroid-fragments

提问by pawas kr. singh

picture

图片

This view is not constrained, it only has design time positions, so it will jump to (0,0) unless you add constraints

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

The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with design time attributes (such as layout_editor_absolute X.) These attributes are not applied at run time, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections

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

采纳答案by IoT_Newbie

It is very simple to solve this problem. Just click on the widget (for example button or textbox, etc.) then click on the "Infer constraints" Button. You can see in the attached picture or this Youtube link: https://www.youtube.com/watch?v=uOur51u5Nk0

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

Infer constraints picture

推断约束图片

回答by Firoz Memon

You might have widgets with attributes:

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

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

toolsnamespace is used at development time only, and will be removed while installing apk, so all you layouts might appear at position TOP-LEFT one above other. View Tools Attributes Reference

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

To fix this: You should make use of Layout Constraints like:

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

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

You can go through the doc of ConstraintLayoutand Build a Responsive UI with ConstraintLayoutfor more details

您可以阅读ConstraintLayout的文档并使用 ConstraintLayout 构建响应式 UI 以了解更多详细信息

EDIT:

编辑:

From the pictureyou posted, I tried to add appropriate constraints such that the TextView is in Center position using following code:

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

回答by Mandarkapse17

"Missing constraints in ConstraintsLayout".This error occurs due to the undefined Constraints.To fix this error click on the Widget(ex text,button etc) and then click on the infer constraints which is above the the activity screen.Hope this solves your query

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