ConstraintLayout Problems using <include>

Refresh

December 2018

Views

1.6k time

4

I am trying to figure out why this does not work. I am adding nothing but two <include> sections in a ConstraintLayout and the layout is not following any of the constraints that I set up. I am trying to begin migrating to the use of ConstraintLayout as my go-to layout, but things like this keep pushing me back to RelativeLayout and LinearLayout.

Here is the top level layout file (the ConstraintLayout), showing some of the constraints that are not working:

<?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:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <include
        android:id="@+id/includeButton"
        layout="@layout/include_button_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <include
        android:id="@+id/includeText"
        layout="@layout/include_text_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout

Here is the first included layout (include_button_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/include_button_panel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch me!" />

</merge>

Here is the second included layout (include_text_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/include_text_panel_text"
        android:layout_width="wrap_content"
        android:background="#7986cb"
        android:layout_height="wrap_content"
        android:text="This is the text panel"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</merge>

2 answers

4

попробуйте это Удалить <merge>тег из обоих ваших включенных макетов

<?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:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <include
        android:id="@+id/includeButton"
        layout="@layout/include_button_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <include
        android:id="@+id/includeText"
        layout="@layout/include_text_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
8

Удалите <merge>тег из обоих ваших включенных макетов.

Когда вы specifiy атрибутов на <include>тег, система будет применять эти атрибуты в корневом зрение прилагаемого макета. <merge>Тег представляет собой специальный элемент , который позволит вам иметь несколько представлений в макете , не имея вид корня. Так что все ваши ограничения вышвырнут, когда ваш включен макет использует <merge>тег. К счастью, оба ваших включены макеты имеют только один вид внутри слияния, так что вы можете просто удалить слияния в целом.