2016-08-14 21 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に変更します。現在、高さに循環依存があります。

[EDIT]

我々はソースでスケーリングする画像のプロパティ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

あなたの解決策は動作しません。 btw私はスクリーンショットを変更しました。以前のバージョンでは明るい青色のハイライトが間違っていたためです。 –

+0

@MosesAprico上記の更新を確認してください。 – Shaishav

+0

愚かなアンドロイド・デュ。とにかく彼らはどうにかデフォルトとして奇妙な設定を作ることができます。 :/ –

関連する問題