So fügen Sie ein Beschränkungslayout in ein anderes Beschränkungslayout ein und legen eine Beschränkung zwischen jedem fest

Lesezeit: 9 Minuten

Benutzer-Avatar
Kelian

Ich verwende ConstraintLyout v 1.0.1.

Ich möchte in meine XML-Datei ein untergeordnetes ConstraintLayout einfügen, das einem Teil meines globalen Layouts entspricht (das selbst ein ConstraintLayout ist). Ich habe das Layout in zwei XML-Dateien aufgeteilt, um diesen Teil an anderer Stelle zu verwenden

Ich habe dies versucht, aber ich habe keine Kontrolle darüber, wo ich mein untergeordnetes Constraint-Layout im übergeordneten Element platzieren soll. Ich frage mich, ob ich alles in derselben XML-Datei platzieren muss oder ob es eine Lösung gibt, separate Dateien zu verwenden.

tmp_1.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LABEL1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        />
    <TextView
        android:id="@+id/label_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LABEL2"
        app:layout_constraintStart_toStartOf="@id/label"
        app:layout_constraintEnd_toEndOf="@id/label"
        app:layout_constraintTop_toBottomOf="@id/label"
        android:layout_marginTop="16dp"
        />

    <include layout="@layout/tmp_2" />
</android.support.constraint.ConstraintLayout>

tmp_2.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/view_80"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="80th element"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="10dp"
        android:layout_marginStart="12dp"
        />
</android.support.constraint.ConstraintLayout>

Das Ergebnis ist folgendes

Tatsächliche Ergebnis

Aber ich möchte, dass es so ist

Erwartetes Ergebnis

Ich habe dies versucht, aber es funktioniert nicht

<include 
    app:layout_constraintTop_toBottomOf="@id/label_2"
    layout="@layout/tmp_2" />

  • Sie können die Einschränkungsparameter zum Root-Tag des enthaltenen Layouts hinzufügen.

    – opLW

    22. April 2021 um 2:05 Uhr

Benutzer-Avatar
Kelian

Habe tatsächlich eine Lösung gefunden. Android Studio vervollständigt nicht automatisch ConstraintLayout-Parameter in einem Include-Tag, aber sie wirken sich darauf aus, solange Sie dem Include eine Größe zuweisen.

<include
        layout="@layout/tmp_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/label_2"
        />

  • Glaubst du nicht, dass es den Zweck der Erstellung einer Benutzeroberfläche ohne verschachtelte Layouts zunichte macht, wenn wir das Constraint-Layout in ein anderes Constraint-Layout einfügen?

    – Nouman Bhatti

    20. März 2018 um 9:49 Uhr

  • @NoumanBhatti Sie können sich irren. ConstraintLayout reduziert verschachtelte Layouts und erstellt nicht alle Layouts ohne verschachtelte Layouts. Include ist am Wiederverwenden von Layouts interessiert, und das kann ConstraintLayout nicht. Wenn das Einfügen eines Include Ihr Layout flexibler machen kann, können Sie dies bedenkenlos tun.

    – Allen Vork

    18. April 2018 um 7:18 Uhr

  • Für alle anderen, die hinter mir kommen, wenn dies nicht funktioniert, könnte es daran liegen, dass Sie Ihre Includes in a verschachtelt haben <merge /> Schild. Als ich das entfernte und das Constraint-Layout dem übergeordneten Layout überließ, konnte ich die Positionierung und die Größe anpassen

    – Hallenmeister

    7. August 2018 um 10:24 Uhr

  • Auch für mein Setup konnte ich wrap_content nicht für die Höhe verwenden, ich musste ihm einen expliziten dp-Wert geben.

    – Hallenmeister

    7. August 2018 um 10:41 Uhr

  • Diese Lösung funktionierte auch für eine andere Situation. Es scheint, dass Android Studio erwartet, wenn Sie eines der angeben app:layout_constraint* Parameter, die Sie auch angeben müssen android:layout_width und android:layout_height für die <include>

    – Michael Osofsky

    6. September 2019 um 18:01 Uhr

Um ein Beschränkungslayout einzufügen und es nach Bedarf einzuschränken, muss man dem eingeschlossenen Layout Breite und Höhe wie folgt geben:

<include
        android:id="@+id/shop_card_layout"
        layout="@layout/shop_card_one"
        android:layout_height="wrap_content"
        android:layout_width="300dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="@id/heading_tv"
        app:layout_constraintTop_toBottomOf="@+id/heading_tv" />

  • @DhruvamSharma Wenn Sie die ID neu definieren, erhalten Sie diesen Fehler stackoverflow.com/questions/46045377/…

    – Deepak Kumar

    29. August 2019 um 8:37 Uhr

  • korrekte Antwort! Ich habe layout_width und layout_height hinzugefügt, constains gesetzt (oben und unten in meinem Fall) und es funktioniert.

    – oxidiert

    27. Oktober 2020 um 19:05 Uhr


  • Dies ist die richtige Antwort, Sie müssen dem enthaltenen Layout Höhe und Breite hinzufügen, müssen es nicht in ein anderes übergeordnetes Layout einschließen.

    – Nezneika

    13. Dezember 2020 um 8:00 Uhr


Das könntest du vermeiden ConstraintLayout Einschränkungen beim Include-Element. Ich habe gerade <include/> es wie es ist.

MainActivity-Layoutdatei:

<?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">

    <include
        android:id="@+id/toolbarLayout"
        layout="@layout/layout_toolbar" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="CONTENTS"
        app:layout_constraintBottom_toBottomOf="@+id/footerLayout"
        app:layout_constraintEnd_toEndOf="@+id/footerLayout"
        app:layout_constraintStart_toStartOf="@+id/footerLayout"
        app:layout_constraintTop_toTopOf="@+id/footerLayout" />

    <include
        android:id="@+id/footerLayout"
        layout="@layout/layout_footer" />

</android.support.constraint.ConstraintLayout>

ToolBar-Layout-Datei:

<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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:id="@+id/toolbarTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/hidden"
            android:textColor="@android:color/white"
            tools:layout_editor_absoluteX="192dp"
            tools:layout_editor_absoluteY="19dp" />

    </android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>

  • Würde die Verwendung eines Include nicht ein Beschränkungslayout innerhalb des übergeordneten Beschränkungslayouts erstellen? Dann ist es sinnvoll, die zu verwenden include da Sie nicht mehr die flache Ansichtshierarchie des Constraint-Layouts haben

    – 11m0

    8. März 2019 um 23:57 Uhr

  • Durch including einem anderen Layout können Sie die enthaltenen Layouts in anderen wiederverwenden activities.

    – Wenig

    9. März 2019 um 5:38 Uhr

  • Der Vorteil besteht also darin, eine bestimmte Layoutdatei wiederverwenden zu können, der Nachteil besteht jedoch darin, dass Sie durch potenziell verschachtelte Layouts Leistungseinbußen erleiden. Das stimmt?

    – 11m0

    10. März 2019 um 0:16 Uhr


  • Ja, du hast ein Leistungsproblem. Aber ich glaube, ich habe die Lösung für das ganze Problem gefunden. Die Lösung könnte sein parentTag wie hier beschrieben (android.jlelse.eu/android-dev-tip-5-55226527e780). Ich werde es testen, wenn ich Zeit habe, oder Sie können es testen und die Community wissen lassen.

    – Wenig

    11. März 2019 um 4:48 Uhr

  • Das parentTag in Tools ist nur für das, was Sie auf der Design-Registerkarte von Android Studio sehen, es hat keine Auswirkung zur Laufzeit

    – Nikos Hidalgo

    10. September 2019 um 14:38 Uhr

Um ein Beschränkungslayout in ein anderes Beschränkungslayout einzuschließen, um ein weiteres Beschränkungslayout für jedes übergeordnete Element des Einschlusslayouts zu verwenden … Wie unten:

<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"
android:background="@drawable/new_landing_bg"
tools:context=".activity.DesignTestActivity">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <include layout="@layout/common_footer_layout" />

</android.support.constraint.ConstraintLayout>

Dies ist Arbeit an meinem XML-Layout. Genießen Sie Code.

Benutzer-Avatar
Schiwam

Wir können a verwenden Layout Schild bei Verwendung des Constraint-Layouts. Für lineares Layout können wir das Layout direkt einbeziehen.

Ich hatte eine andere Anforderung, bei der ich eine Registerkartenansicht unter der Symbolleiste habe. Ich hatte die Symbolleistenhöhe auf 0 dp eingestellt und eine Ansichtsdatei unterhalb der Registerkartenansicht hinzugefügt, sodass sowohl die Registerkartenansicht als auch die Symbolleiste als eine Einheit erscheinen.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs_container"
        style="@style/tab_layout_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:tabGravity="fill"
        app:tabIndicatorFullWidth="false"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/text_black_app"
        app:tabTextColor="@color/txt_gray_contact_lbl" />

    <LinearLayout
        android:id="@+id/layout_elevation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/tabs_container">

        <include layout="@layout/gray_view_for_elevation" />
    </LinearLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="100"
        app:layout_constraintTop_toBottomOf="@+id/layout_elevation_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

Dieser Code ist für das lineare Layout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        style="@style/tab_layout_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/dp_4"
        app:tabGravity="fill"
        app:tabIndicatorFullWidth="false"
        app:tabMode="scrollable"/>

    <include
        android:id="@+id/layout_elevation_view"
        layout="@layout/gray_view_for_elevation" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_gray" />

</LinearLayout>

Meine Ansichtsdatei

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#80bbbbbb" />

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#60bbbbbb" />

<View
    android:layout_width="wrap_content"
    android:layout_height="@dimen/dp_1"
    android:background="#50bbbbbb" />

Hoffe, das wird helfen.

1284010cookie-checkSo fügen Sie ein Beschränkungslayout in ein anderes Beschränkungslayout ein und legen eine Beschränkung zwischen jedem fest

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy