与
ConstraintLayout
通过将高度设置为,可以匹配其他视图的高度
0dp
我删掉了布局的其他部分,所以您必须添加它们,但下面是图像和文本的代码。
Produce this
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/cardview"
app:cardBackgroundColor="#FF6B6B"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
tools:src="@tools:sample/avatars"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="@id/text"
app:layout_constraintEnd_toEndOf="@id/text"
app:layout_constraintTop_toTopOf="@id/text"
app:layout_constraintBottom_toBottomOf="@id/text" />
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lineSpacingExtra="4dp"
android:maxLines="4"
android:text="This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>