web-dev-qa-db-ja.com

Android studioが「constraintlayoutの制約がありません」というエラーを表示するのはなぜですか?

写真

このビューは制約されていません。設計時の位置のみがあるため、制約を追加しない限り、(0,0)にジャンプします。

レイアウトエディターを使用すると、ウィジェットをキャンバス上のどこにでも配置でき、現在の位置をデザイン時の属性(layout_editor_absolute Xなど)で記録します。これらの属性は実行時に適用されないため、デバイスにレイアウトをプッシュすると、ウィジェットは、エディターに表示される場所とは異なる場所に表示される場合があります。これを修正するには、エッジ接続からドラッグして、ウィジェットに水平拘束と垂直拘束の両方があることを確認します

6
pawas kr. singh

この問題を解決するのは非常に簡単です。ウィジェット(ボタンやテキストボックスなど)をクリックし、[制約の推定]ボタンをクリックするだけです。添付の写真またはこのYouTubeリンクで確認できます: https://www.youtube.com/watch?v=uOur51u5Nk

Infer constraints picture

26
IoT_Newbie

次の属性を持つウィジェットがある場合があります。

    tools:layout_editor_absoluteX="someValue"
    tools:layout_editor_absoluteY="someValue"

tools名前空間は開発時にのみ使用され、apkのインストール中に削除されるため、すべてのレイアウトがTOP-LEFTの上下に表示される可能性があります。表示 ツール属性リファレンス

これを修正するには:次のようなレイアウト制約を使用する必要があります。

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

詳細については、 ConstraintLayout および ConstraintLayoutを使用したレスポンシブUIの作成 のドキュメントを参照してください。

編集:

あなたが投稿した 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>
2
Firoz Memon