首页 > 解决方案 > 卡片视图中的图像未完全填充

问题描述

我有这个带有图像的卡片视图,但问题是,图像没有填满卡片,我不确定为什么会发生这种情况(改变宽度似乎没有任何影响)。

这是它的外观: 在此处输入图像描述

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="10dp"
   android:orientation="vertical">

   <androidx.cardview.widget.CardView
       android:layout_width="180dp"
       android:layout_height="130dp"
       app:cardCornerRadius="16dp"
       app:strokeColor="@color/white">

       <ImageView
           android:id="@+id/iv_news"
           android:layout_width="250dp"
           android:layout_height="130dp"
           android:layout_marginStart="10dp"
           android:background="@drawable/card_background_shape"
           android:scaleType="centerCrop" />
   </androidx.cardview.widget.CardView>

   <TextView
       android:id="@+id/tv_title"
       android:layout_width="180dp"
       android:layout_height="wrap_content"
       android:layout_marginStart="10dp"
       android:layout_marginTop="5dp"
       android:gravity="center"
       android:text="@string/news_title"
       android:textAlignment="center"
       android:textAppearance="?android:attr/textAppearanceSmall"
       android:textColor="@color/black" />

</LinearLayout>



标签: androidkotlin

解决方案


您需要android:layout_marginStart="10dp"在您的ImageView.

它应该看起来像这样:

<ImageView
           android:id="@+id/iv_news"
           android:layout_width="250dp"
           android:layout_height="130dp"
           android:background="@drawable/card_background_shape"
           android:scaleType="centerCrop" />

结果:

在此处输入图像描述


推荐阅读