android - ViewPager wrap_content 内的 ConstraintLayout 不起作用
问题描述
我创建了一个 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 内部项目的布局是:
<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>
如您所见,每个视图或其父视图都有不同的背景。如果我运行该应用程序,我会看到:
背景是红色的,这意味着 ViewPager 项目中的 ConstraintLayout 被拉伸到屏幕的整个高度,尽管wrap_content
. 任何人都知道,如何将项目缩小到内部图像的大小?
PS:我在这里使用ConstraintLayout,因为我需要将图像尺寸保持在纵横比“3:1”
解决方案
设置所有约束,parent
然后0dp
在高度或宽度中使用,而不是 `wrap_content'
推荐阅读
- javascript - 如何阻止我的 React Native 测试在 Node Modules 中的 Flow 类型上轰炸?
- python - 在python中最小化具有3个约束的6维问题
- postgresql - 传递根作为函数属性条件查询
- c++ - C++ 实验文件系统库不完整?
- python - 使用python定义&返回函数
- flutter - 为什么我在使用 twitter_api 包时不断收到错误 (404)?
- android - MainActivity 不是封闭类,“this”会导致构造函数错误
- machine-learning - 加载数据文件时 ML.Net“数据加载期间未遇到有效实例”错误
- react-native - 为什么要用 npx 初始化一个 react-native 项目?
- ffmpeg - ffmpeg 将具有多个音频的 mkv 映射到具有更多音频的 mkv