2017-12-18 632 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

我現在添加圖片,你可以看到.. –