2017-07-27 86 views
2

我正在用android studio創建我的第一個應用程序,這是我的第一個問題:約束佈局不起作用,可能是設計編輯器中的錯誤?

我想嘗試ConstraintLayout。我用Design Editor編寫了一個帶有ConstraintLayout的佈局(點擊它們)。當我在Android模擬器中嘗試佈局時,所有按鈕都移動到左上角:( 除了當我創建我的第一個項目時自動生成的「Hello World」標籤

標籤和按鈕是,一些以app:layout_constraint ....開頭的代碼行缺失。你可以在代碼中看到它。

我在做什麼錯誤,或者它是一個錯誤? 我會很高興回答!:)

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="com.example.u0017007.coffeecounter.MainActivity"> 

    <TextView 
     android:layout_width="136dp" 
     android:layout_height="30dp" 
     android:text="Hello World!" 
     android:textSize="24sp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.234" /> 

    <Button 
     android:id="@+id/buttonAddCoffee" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:freezesText="false" 
     android:text="@string/add_coffee" 
     tools:layout_editor_absoluteX="16dp" 
     tools:layout_editor_absoluteY="231dp" /> 

    <Button 
     android:id="@+id/buttonRemoveCoffee" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/remove_coffee" 
     tools:layout_editor_absoluteX="236dp" 
     tools:layout_editor_absoluteY="231dp" /> 

</android.support.constraint.ConstraintLayout> 
+0

使用RelativeLayout而不是ConstraintLayout。 – Abhi

+1

[左上角的ConstraintLayout視圖]的可能重複(https://stackoverflow.com/questions/42594033/constraintlayout-views-in-top-left-corner) –

回答

2

從上面的代碼看來,您沒有添加任何約束到按鈕這就是爲什麼他們移動到左上角。

<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="136dp" 
     android:layout_height="30dp" 
     android:text="Hello World!" 
     android:textSize="24sp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.234" /> 

    <Button 
     android:id="@+id/buttonAddCoffee" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:freezesText="false" 
     android:text="ADD coffee" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="276dp" 
     app:layout_constraintRight_toLeftOf="@+id/buttonRemoveCoffee" 
     android:layout_marginRight="82dp" 
     android:layout_marginLeft="24dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="1.0" /> 

    <Button 
     android:id="@+id/buttonRemoveCoffee" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="remove coffee" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="276dp" 
     android:layout_marginRight="24dp" 
     app:layout_constraintRight_toRightOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

請參考波紋管連接 -

https://www.youtube.com/watch?v=z53Ed0ddxgM

2

tools:layout_editor_absoluteXtools:layout_editor_absoluteY僅用於預覽,像所有tools:XXXX
您需要爲您的視圖添加約束條件。你可以添加XML,或者你可以用可視化編輯器來完成。

有一個很好的website,解釋所有關於ConstraintsLayout

順便說一下,如果您沒有設置約束,Android Studio會爲您提供一個錯誤This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints

0

工具:layout_editor_absoluteX = 「236dp」

工具:layout_editor_absoluteY = 「231dp」

這些被使用的工作室只有圖形編輯器進行渲染。在運行時,

由於未設置約束,視圖採用默認位置(0,0)。

嘗試在按鈕上設置一些約束並執行