2017-12-18 286 views
-7

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

这种观点是不受限的,它只有设计时的位置,所以它会跳转到(0,0),除非你添加约束

布局编辑器允许任何地方小工具在画布上,它使用设计时间属性(如layout_editor_absolute X)记录当前位置。这些属性在运行时不适用,因此,如果在设备上推送布局,则小部件可能会显示在与编辑器中显示不同的位置。为了解决这个问题,确保一个widget从边缘连接拖动

+1

你能在这里发表您的布局文件? – ViramP

+0

你是否复制文本而不是提问? – Stefan

+0

我认为你应该从约束布局如何工作的快速教程开始https://www.youtube.com/watch?v=2FOFd77o7eg – Bmbariah

回答

2

解决此问题非常简单。只需点击小部件(例如按钮或文本框等),然后点击“Infer constraints”按钮。 可以在附加的图片或本Youtube链接见:https://www.youtube.com/watch?v=uOur51u5Nk0

Infer constraints picture

0

你可能有小部件与属性具有水平和垂直两个约束条件:

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

它不工作.. –

+0

你可以更新你所尝试过的,所以它会更容易帮助你 –

+0

我现在添加图片,你可以看到.. –