android如何根据里面的imageView调整cardView?
How to adjust cardView according to imageView inside in android?
我想让我的cardView根据里面imageView的宽高自己调整,达到这样的效果
但是我在设计方面并没有真正的经验,所以我遇到了麻烦。
这是我使用的代码
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<TextView
android:id="@+id/imageTimeMessageSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9:16 AM"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cardView4"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.98" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView4"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_marginEnd="12dp"
app:cardBackgroundColor="?attr/colorPrimary"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageView
android:id="@+id/imageMessageSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher_background"
tools:srcCompat="@drawable/default_profile" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
该代码适用于大多数图像,但某些图像在 cardView
上留下 space
注意:我也想有一定的限制,这样 cardView 就不会超出这个限制
只需像这样制作 cardView 高度和宽度 wrap_content:
<androidx.cardview.widget.CardView
android:id="@+id/cardView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
app:cardBackgroundColor="?attr/colorPrimary"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
所以在cardView中
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
如果你添加它,它就会如你所愿。
您可以将ImageView
的width/height设置为与CardView
相同,并使用android:scaleType="fitCenter"
<ImageView
android:id="@+id/imageMessageSent"
android:layout_width="230dp"
android:layout_height="230dp"
android:background="@drawable/abdeen_04"
android:padding="6dp"
android:scaleType="fitCenter" />
我想让我的cardView根据里面imageView的宽高自己调整,达到这样的效果
但是我在设计方面并没有真正的经验,所以我遇到了麻烦。 这是我使用的代码
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<TextView
android:id="@+id/imageTimeMessageSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9:16 AM"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cardView4"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.98" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView4"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_marginEnd="12dp"
app:cardBackgroundColor="?attr/colorPrimary"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageView
android:id="@+id/imageMessageSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher_background"
tools:srcCompat="@drawable/default_profile" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
该代码适用于大多数图像,但某些图像在 cardView
上留下 space注意:我也想有一定的限制,这样 cardView 就不会超出这个限制
只需像这样制作 cardView 高度和宽度 wrap_content:
<androidx.cardview.widget.CardView
android:id="@+id/cardView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
app:cardBackgroundColor="?attr/colorPrimary"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
所以在cardView中
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
如果你添加它,它就会如你所愿。
您可以将ImageView
的width/height设置为与CardView
相同,并使用android:scaleType="fitCenter"
<ImageView
android:id="@+id/imageMessageSent"
android:layout_width="230dp"
android:layout_height="230dp"
android:background="@drawable/abdeen_04"
android:padding="6dp"
android:scaleType="fitCenter" />