CardView无法缩放图像

问题描述

我正在使用Cardview,它在Android中具有用于RecyclerView的ImageView。不幸的是,CardView无法缩放图像,这意味着较小的图像会导致RecyclerView中的较小项目。在这里,您有一个屏幕截图,其中使用了两个不同的ImageResources(一个一个小):

enter image description here

这是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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...