2016-08-14 128 views
2

下面的屏幕快照,我将使用ImageView的图像插入到CardView中。如何摆脱CardView中ImageView周围的空白区域

问题是,我似乎无法摆脱图像下面的空白空间。 (由浅蓝色突出显示)

我试过a solution that works,但它需要我明确指定图像高度,我尽量不要这样做,因为源可以是各种各样的,而且我需要图像灵活地向上/向下缩放(具有相同的宽高比),具体取决于卡的宽度。

以下是截图:

enter image description here

而这里的代码片段:

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="2dp" 
    card_view:cardElevation="2dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="7dp"> 
     <TextView 
      android:id="@+id/caption_text" 
      android:text="Hello World!" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:scaleType="fitStart" 
      android:layout_below="@id/caption_text" 
      android:src="@drawable/food"/> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

那么,有没有什么办法让CardView自动换行的图像周围,(不留任何空的空间)并且没有明确指定图像高度?

回答

2

<RelativeLayout>高度更改为wrap_content。目前,它具有高度的循环依赖性。

[编辑]

我们需要设置该属性android:adjustViewBounds="true"与源缩放图像。其默认为false

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="2dp" 
    card_view:cardElevation="2dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="7dp"> 
     <TextView 
      android:id="@+id/caption_text" 
      android:text="Hello World!" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:adjustViewBounds="true" 
      android:layout_below="@id/caption_text" 
      android:src="@drawable/food"/> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 
+0

尝试过,但您的解决方案无法正常工作。顺便说一句,我改变了截图,因为之前显示错误的浅蓝色突出显示。 –

+0

@MosesAprico检查我的更新 – Shaishav

+0

愚蠢的机器人。无论如何,他们怎么能够做出一些奇怪的配置。 :/ –