2017-07-27 10 views
2

私はアンドロイドスタジオで初めてのアプリを作成しています。これは私の最初の問題です。制約レイアウトが機能しない、おそらくデザインエディタのバグ?

私はConstraintLayoutを試してみたいと思います。デザインエディタで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をprocedd。 – 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)をとります。

は、ボタンの上にいくつかの制約を設定してみてくださいと

関連する問題