【问题标题】:CardView does not scale ImagesCardView 不缩放图像
【发布时间】:2021-01-03 04:12:16
【问题描述】:

我正在使用一个 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>

我尝试对 ImageView(和 LinearLayout)的属性使用“wrap_content”,并尝试包含属性“android:scaleType="fitXY”,但它没有帮助,似乎也没有任何效果。

有人介意帮助我吗?非常感谢您的每一条评论,并非常感谢您的帮助。

【问题讨论】:

  • 制作CardView 宽度match_parent

标签: android android-recyclerview android-cardview


【解决方案1】:

您需要为任何视图提供固定高度,例如

<?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>

【讨论】:

  • 感谢 umar 的回答。基本上我必须使用“match_Parent”而不是“wrap_content”
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 2021-10-15
  • 2015-09-27
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多