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

시도했지만 솔루션이 작동하지 않습니다. btw 나는 이전에 잘못된 연한 파란색으로 강조 표시했기 때문에 스크린 샷을 변경했습니다. –

+0

@MosesAprico 위의 업데이트를 확인하십시오. – Shaishav

+0

바보 android duh. 어쨌든 그들은 디폴트로 이상한 설정을 어떻게 만들 수 있습니까? :/ –

관련 문제