ViewPager wrap_content 内的 ConstraintLayout 不工作

ConstraintLayout inside of ViewPager wrap_content not working

我创建了一个 ViewPager 并将其放在 ConstraintLayout 中:

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/green">

    <ViewPager
        android:id="@+id/teaser_view_pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:background="@color/blue"
        android:currentPage="@={viewModel.currentItem}"
        bind:adapter="@{viewModel.adapter}"
        bind:withAnimation="@{viewModel.changeWithAnimation}"
        bind:layout_constraintStart_toStartOf="parent"
        bind:layout_constraintTop_toTopOf="parent"
        bind:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

并且 ViewPager 里面的 item 的布局是:

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/red">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:background="@color/black"
        bind:imageUrl="@{viewModel.imagePath}"
        bind:layout_constraintDimensionRatio="3:1"
        android:adjustViewBounds="true" />

</androidx.constraintlayout.widget.ConstraintLayout>

如您所见,每个视图或其父视图都有不同的背景。如果我 运行 该应用程序,我会看到:

背景是红色的,这意味着尽管wrap_content,ViewPager项目中的ConstraintLayout被拉伸到整个屏幕的高度。 有谁知道,如何将项目缩小到里面的图像大小?

P.S.: 我在这里使用 ConstraintLayout,因为我需要保持图像大小的纵横比为“3:1”

将所有约束设置为 parent 然后在高度或宽度中使用 0dp 而不是 `wrap_content'