2017-07-17 17 views
1

我是Android Studio的新手,當我創建我的應用程序時,所有元素都相互重疊。我不知道如何編輯此代碼。請給我一個詳細的描述。在我的android工作室模擬器互相重疊的元素

佈局編輯器和仿真結果

enter image description here

代碼佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="schemas.android.com/apk/res/android"; 
    xmlns:app="schemas.android.com/apk/res-auto"; 
    xmlns:tools="schemas.android.com/tools"; 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.first.anew.MainActivity"> 

    <TextView 
     android:layout_width="216dp" 
     android:layout_height="60dp" 
     android:text="Hello World!" 
     tools:layout_editor_absoluteY="16dp" 
     tools:layout_editor_absoluteX="61dp" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="100dp" 
     android:layout_height="45dp" 
     android:text="Login" 
     tools:layout_editor_absoluteY="343dp" 
     tools:layout_editor_absoluteX="177dp" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="215dp" 
     android:layout_height="65dp" 
     android:text="username" 
     tools:layout_editor_absoluteY="117dp" 
     tools:layout_editor_absoluteX="61dp" /> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline" 
     app:layout_constraintGuide_begin="20dp" 
     android:orientation="horizontal" /> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="217dp" 
     android:layout_height="57dp" 
     android:ems="10" 
     android:inputType="textPassword" 
     tools:layout_editor_absoluteY="218dp" 
     tools:layout_editor_absoluteX="61dp" /> 
</android.support.constraint.ConstraintLayout> 
+0

嘗試改變'工具:''到應用程序:'如果還是不行使用推斷約束選項 –

回答

0

把整個代碼。如果你正在使用Relativelayout,這會發生。更改爲Linearlayout在頂部我的意思是最底部的元素,並把方向垂直

+0

,如果這不能解決問題,請分享整個代碼和設計。 –

+0

<?xml version =「1.0」encoding =「utf-8」?> _ **在它們之間的上述碼是有** _ Gtv

2

您的模擬器屏幕和佈局編輯器是不同的,因爲tools命名空間。簡而言之,它是一種幫助您更好地可視化編寫代碼的工具。該工具還會生成約束條件,如tools:layout_editor_absoluteY="343dp",它通知佈局編輯器應該如何繪製視圖。問題是這個約束不會出現在你的應用程序代碼中,導致所有視圖重疊時的怪異行爲(因爲上面提到的約束沒有在應用程序代碼中使用,我們可以推斷沒有約束)。

既然你是初學者,我會建議以LinearLayoutRelativeLayout開頭,因爲我認爲ConstraintLayout很難理解,使用和調試。

-1

您可以點擊推斷約束按鈕click on this

並運行程序again..it將解決你的問題