<include> 布局没有合适的尺寸

问题描述

我正在创建一个 layout_banner.xml,它被多个其他布局引用,高度为 80dp。

layout_banner.xml:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#4455ff"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Banner test"
        android:textSize="24sp"
        android:textColor="@color/white"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

预览:

Banner layout

但是,一旦我使用 将此布局添加到另一个布局中,高度就不是预期值:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        android:id="@+id/banner_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/layout_banner"
        app:layout_constraintTop_toTopOf="parent"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/planner_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/banner_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        android:padding="16dp"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

预览:

enter image description here

当使用 include 引用时,为什么横幅短于 80dp?包含对象的高度设置为 wrap_content,所以 layout_banner.xml 不应该能够占据它需要的尽可能多的高度吗?


更新:感谢到目前为止的回复,从包含布局中删除宽度和高度修复了尺寸问题,但不幸的是,这也使得任何约束都被忽略。例如,在下面的布局中,横幅按预期是 80dp,但应该在 recyclerView 下方,而不是上方:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/planner_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="16dp"
        />

    <include
        android:id="@+id/banner_layout"
        layout="@layout/layout_banner"
        app:layout_constraintTop_toBottomOf="@id/planner_recyclerview"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

解决方法

使横幅的高度为 80dp remove layout_width and layout_height of include

,

在您的 activity_main.xml 上,您覆盖了 layout_widthlayout_heightlayout_banner.xml,,请执行以下操作:

<include
    android:id="@+id/banner_layout"
    layout="@layout/item_agregar_pedido"
    app:layout_constraintTop_toTopOf="parent"
    />