我有这个 xml,但仅当我硬调整图像的宽度和高度时才会显示图像。我不确定为什么我在代码中提供的约束不起作用。
<merge
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="wrap_content"
android:layout_height="wrap_content"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<androidx.cardview.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:importantForAccessibility="no"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/placeholder" />
</androidx.cardview.widget.CardView>
</merge>
注意:我无法将其更改为撰写,这是某些遗留代码的一部分。
请注意,
<ImageView
标签的这一部分是无操作的(您可以安全地删除它们)
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
因为这些属性只能通过父子
ConstraintLayout
来读取,这是不正确的,它肯定是CardView
...以及CardView
的父级,好吧,这取决于膨胀的位置,也许CardView
应该有这样的参数,在由 ConstaintLayout
严格托管时将会/应该工作