使用<include>的ConstraintLayout问题

Mik*_*ike 8 android android-layout android-constraintlayout

我想弄清楚为什么这不起作用.我只添加了两个<include>部分,ConstraintLayout并且布局不遵循我设置的任何约束.我想开始迁移到使用ConstraintLayout作为我去到布局,但像这样的事情不断推动我回RelativeLayoutLinearLayout.

这是顶级布局文件(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"
    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
Run Code Online (Sandbox Code Playgroud)

这是第一个包含的布局(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>
Run Code Online (Sandbox Code Playgroud)

这是第二个包含的布局(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>
Run Code Online (Sandbox Code Playgroud)

Ben*_* P. 9

<merge>从两个包含的布局中删除标记.

<include>标记上指定属性时,系统会将这些属性应用于包含布局的根视图.该<merge>标签是为了让你在你的布局多个视图的特殊元素,而不具有根部视图.因此,当您包含的布局使用<merge>标记时,所有约束都会被抛弃.幸运的是,两个包含的布局在合并中只有一个视图,因此您可以完全删除合并.

  • 对于任何想知道的人,我的问题的答案是我没有在父布局的&lt;include&gt;声明中包含android:layout_width和android:layout_height。/sf/ask/3053795601/ (3认同)
  • 当您不使用合并标签但 `&lt;include&gt;` 布局仍然忽略其父级中的约束时,您会怎么做?我有这个确切的问题。 (2认同)

Nil*_*hod 5

尝试此操作<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>
Run Code Online (Sandbox Code Playgroud)