使用<include>的ConstraintLayout问题

时间:2017-09-19 02:00:45

标签: 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

这是第一个包含的布局(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>

这是第二个包含的布局(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>

3 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分: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>

答案 2 :(得分:0)

正如科迪(Cody)在评论中提到的那样,问题可能是您没有为包含的布局设置layout_widthlayout_height ,女巫没有产生任何编辑错误,但是约束不再起作用。