【问题标题】:<include> layout does not have the right size<include> 布局没有正确的大小
【发布时间】:2021-06-10 21:01:29
【问题描述】:

我正在创建一个 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>

预览:

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

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>

预览:

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


更新:感谢到目前为止的响应,从包含布局中删除宽度和高度修复了大小问题,但不幸的是,这也使得任何约束都被忽略了。例如,在下面的布局中,banner 按预期是 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>

【问题讨论】:

    标签: android android-layout android-constraintlayout android-wrap-content android-include


    【解决方案1】:

    使横幅的高度为80dp 删除包含的layout_widthlayout_height

    【讨论】:

    • 谢谢,这确实解决了问题,但它会产生另一个问题。当我删除宽度和高度时,任何约束都会被忽略,例如,如果我尝试将横幅放在固定高度的 recyclerView 下方。有没有办法避免这种情况?
    • 尝试将高度设置在横幅上方而不是横幅上方
    • 这样
    • 我明白了,但这违背了使用 的某些目的,因为我希望只在一个地方定义高度,而不是每次引用横幅时。但是,我想我可以在 dimens.xml 中设置高度并让 块将其用于高度,所以我会接受你的回答。感谢您的帮助!
    • 不客气! PS:如果你使用LinearLayout你不会有这个问题
    【解决方案2】:

    在您的activity_main.xml 上,您正在覆盖您的layout_banner.xml,layout_widthlayout_height,只需这样做:

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

    【讨论】:

    • 谢谢,这确实解决了问题,但它会产生另一个问题。当我删除宽度和高度时,任何约束都会被忽略,例如,如果我尝试将横幅放在固定高度的 recyclerView 下方。有没有办法避免这种情况?
    • 你能贴一张你想要的设计图片吗?还是编辑后的 ​​.xml ?
    • 我已使用具有约束问题的新 XML 更新了问题。
    • 从其他帖子看来,不设置宽度和高度似乎是不可能的,见this
    猜你喜欢
    • 2019-07-19
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多