2016-08-14 59 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

愚蠢的機器人。無論如何,他們怎麼能夠做出一些奇怪的配置。 :/ –