Идентификатор в теге <include> разрушает макет

У меня есть макет в отдельном файле xml, который включается в другие файлы. Я хочу сослаться на включенный файл, поэтому я установил идентификатор. Но с идентификатором макет становится совершенно неструктурированным. Мини пример:

Родительский макет:

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">

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

</android.support.constraint.ConstraintLayout>

Включенный макет:

<?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"
android:id = "@+id/constraint_parent"
android:layout_width = "match_parent"
android:layout_height = "match_parent">

<TextView
    android:id = "@+id/t1"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "1"
    app:layout_constraintEnd_toStartOf = "@id/t2"
    app:layout_constraintStart_toStartOf = "@id/constraint_parent" />

<TextView
    android:id = "@+id/t2"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "2"
    app:layout_constraintEnd_toStartOf = "@id/t3"
    app:layout_constraintStart_toEndOf = "@id/t1" />

<TextView
    android:id = "@+id/t3"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "3"
    app:layout_constraintStart_toEndOf = "@id/t2"
    app:layout_constraintEnd_toEndOf = "@id/constraint_parent"/>

В результате получается следующий макет: Идентификатор в теге &lt;include&gt; разрушает макет

Но если я изменю тег include на следующий:

<include
    android:id = "@+id/test"
    layout = "@layout/test_include" />

Результат: Идентификатор в теге &lt;include&gt; разрушает макет

Таким образом, схема полностью теряется. Нельзя ли добавить идентификатор в тег включения? Я хочу добавить тег включения два раза, поэтому я хочу добавить два разных идентификатора к двум включениям вместо прямой ссылки на родительский макет включенного макета.

1
0
46
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Это действительно странно. Попробуйте добавить layout_width и layout_height в тег включения, это может исправить это странное поведение.

Ответ принят как подходящий

проблема во включенном макете, используйте вместо него этот xml

<?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"
android:id = "@+id/constraint_parent"
android:layout_width = "match_parent"
android:layout_height = "match_parent">

<TextView
    android:id = "@+id/t1"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "1"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintEnd_toStartOf = "@id/t2"
    app:layout_constraintStart_toStartOf = "parent" />

 <TextView
    android:id = "@+id/t2"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "2"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintEnd_toStartOf = "@id/t3"
    app:layout_constraintStart_toEndOf = "@id/t1" />

 <TextView
    android:id = "@+id/t3"
    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:text = "3"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintStart_toEndOf = "@id/t2"
    app:layout_constraintEnd_toEndOf = "parent"/>
 </android.support.constraint.ConstraintLayout>

Спасибо. Замена @id/constraint_parent на parent в ограничениях исправляет это.

L3n95 19.05.2019 16:45

Другие вопросы по теме