在屏幕截图下方,我使用 ImageView
将图像插入到 CardView
中。
问题是,我似乎无法摆脱图像下方的空白区域。 (用浅蓝色突出显示)
我试过了 a solution that works , 但它要求我明确指定图像高度,我尽量不这样做,因为来源可以是多种多样的,我需要图像根据卡片宽度灵活地放大/缩小(具有相同的纵横比)。
截图如下:
这是代码 fragment :
<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
自动环绕图像(不留任何空白空间)并且无需明确指定图像高度?
最佳答案
更改 <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>
关于android - 如何摆脱 CardView 中 ImageView 周围的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38945117/