Java 此视图不受约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37817537/
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-11 19:42:31  来源:igfitidea点击:

This view is not constrained

javaandroidlayout

提问by Isuru

I get the following error and I am using Android studio 2.2 Preview 3. I searched Google but couldn't find any resources.

我收到以下错误,我使用的是 Android studio 2.2 Preview 3。我搜索了谷歌但找不到任何资源。

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

enter image description here

在此处输入图片说明

<TextView
        android:layout_width="384dp"
        android:layout_height="207dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/calc_default_display"
        android:id="@+id/textView"
        android:background="#ffffff"
        android:layout_margin="0dp"
        android:gravity="bottom|end"
        android:textSize="70sp"
        android:nestedScrollingEnabled="false"
        android:maxLines="2"
        android:maxLength="17"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

回答by onexf

You need to give a value to the constraints manually when using the new layout editor orthe missing constraints can be added automatically by clicking the magic wand button in the toolbar menu of the design preview.

使用新的布局编辑器时,您需要手动为约束赋值,或者可以通过单击设计预览工具栏菜单中的魔术棒按钮自动添加缺少的约束。

回答by Jay Souper

To quicklyresolve this, use this very helpful shortcut in Android Studio:

快速解决此问题,请在 Android Studio 中使用这个非常有用的快捷方式:

Right-click widget-in-question > Constraint Layout > Infer Constraints:

右键单击有问题的小部件 > 约束布局 > 推断约束:

enter image description here

在此处输入图片说明

Thereafter, you can tweak the constraints as described here: https://stackoverflow.com/a/37960888/5556250

此后,您可以按照此处所述调整约束:https: //stackoverflow.com/a/37960888/5556250

Update

更新

This is not correct for the Android Studio v3 and up. As per @purpleladydragons's comment:

这对于 Android Studio v3 及更高版本是不正确的。根据@purpleladydragons 的评论

"Constraint Layout" is not in the dropdown menu. Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button.

“约束布局”不在下拉菜单中。使用设计预览上方工具栏菜单中的魔术棒图标;有“推断约束”按钮。

回答by Satyam Mishra

Right Click in then designing part on that component in which you got error and follow these steps:

右键单击,然后在出现错误的组件上设计零件,然后按照以下步骤操作:

  • [for ex. if error occur in Plain Text]
  • [例如。如果纯文本中发生错误]

[1]

[1]

Plain Text Constraint Layout > Infer Constraints:

纯文本约束布局 > 推断约束:

finally error has gone

最后错误消失了

回答by BertKing

you can try this: 1. ensure you have added: compile 'com.android.support:design:25.3.1'(maybe you also should add compile 'com.android.support.constraint:constraint-layout:1.0.2') 2. enter image description here

你可以试试这个: 1.确保你已经添加: compile 'com.android.support:design:25.3.1'(也许你也应该添加compile 'com.android.support.constraint:constraint-layout:1.0.2') 2.在此处输入图片说明

3.click the Infer Constraints, hope it can help you.

3.点击 Infer Constraints,希望对你有帮助。

回答by Cem U

Solution

解决方案

Just click this and it will be solved

点这个就可以解决

回答by Mohammad Heydari

you can go to the XML filethen focus your mouse cursor into your button, text view or whatever you choose for your layout, then press Alt + Enterto fix it, after that the error will be gone.it works for me.

您可以转到XML file然后将鼠标光标聚焦到您的按钮、文本视图或您为布局选择的任何内容上,然后按Alt + Enter修复它,之后错误就会消失。它对我有用。

回答by pablo.programmer

After clicking on the magic wand icon to infer constraints:

单击魔术棒图标推断约束后:

1) In Projects window, go to Gradle Scripts > build.gradle (Module:app)

1) 在 Projects 窗口中,转到 Gradle Scripts > build.gradle (Module:app)

2) Scroll down to dependencies

2)向下滚动到依赖项

3) Look for implementation 'com.android.support:appcompat-v7:28.0.0-beta03'

3) 寻找实现 'com.android.support:appcompat-v7:28.0.0-beta03'

4) Change this line to implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

4) 将此行更改为 implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

5) A banner should pop up at the top of the window, click Sync Now in the right corner.

5) 应在窗口顶部弹出横幅,单击右上角的立即同步。

This works for Android Studio v3.1 click for image of edit to build.gradle file

这适用于 Android Studio v3.1 单击以获取编辑 build.gradle 文件的图像

回答by tdog

Alright so I know this answer is old, but I found out how to do this for version 3.1.4. So for me this error occurs whenever I put in a new item into the hierarchy, so I knew I needed a solution. After tinkering around for a little bit I found how to do it by following these steps:

好的,所以我知道这个答案很旧,但我发现了如何为 3.1.4 版执行此操作。所以对我来说,每当我将一个新项目放入层次结构时就会发生这个错误,所以我知道我需要一个解决方案。经过一番修补之后,我找到了如何按照以下步骤进行操作:

  1. Right click the object and go to Center.
  1. 右键单击对象并转到中心。

Step 1

第1步

  1. Then select Horizontally.
  1. 然后选择水平。

Step 2

第2步

  1. Repeat those steps, except click Vertically instead of Horizontally.
  1. 重复这些步骤,除了单击垂直而不是水平。

Provided that that method still works, after step two you should see squiggly lines going horizontally across where the item is, and after step three, both horizontally and vertically. After step three, the error will go away!

如果该方法仍然有效,则在第二步之后,您应该会看到横过项目所在位置的波浪线,在第三步之后,水平和垂直方向都应如此。在第三步之后,错误就会消失!

回答by Ekane 3

You just have to right-click on the widget and choose "center" -> "horizontally" and do it again then choose ->"vertically" This worked for me...

您只需要右键单击小部件并选择“居中”->“水平”并再次执行,然后选择->“垂直”这对我有用...

回答by Taimoor Hassan

Right-click on the widget and choose "center" -> "horizontally". Then choose "center"->"vertically".

右键单击小部件并选择“居中”->“水平”。然后选择“居中”->“垂直”。