问题描述
我正在使用Cardview,它在Android中具有用于RecyclerView的ImageView。不幸的是,CardView无法缩放图像,这意味着较小的图像会导致RecyclerView中的较小项目。在这里,您有一个屏幕截图,其中使用了两个不同的ImageResources(一个大一个小):
这是CardView的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
app:cardElevation="0dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="0dp"
card_view:cardBackgroundColor="@android:color/transparent"
app:contentPaddingBottom="0dp"
app:contentPaddingLeft="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:adjustViewBounds="true"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
我尝试将“ wrap_content”用于ImageView(和LinearLayout)的属性,并尝试包含属性“ android:scaleType =“ fitXY”,但它没有帮助,而且似乎没有任何作用。
有人会帮我吗?感谢您的每条评论,也非常感谢您的帮助。
解决方法
您需要为任何视图赋予固定高度
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
app:cardElevation="0dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
app:contentPadding="0dp"
card_view:cardBackgroundColor="@android:color/transparent"
app:contentPaddingBottom="0dp"
app:contentPaddingLeft="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:adjustViewBounds="true"/>
</LinearLayout>
</androidx.cardview.widget.CardView>