android - 如何摆脱 CardView 中 ImageView 周围的空白

标签 android imageview cardview

在屏幕截图下方,我使用 ImageView 将图像插入到 CardView 中。

问题是,我似乎无法摆脱图像下方的空白区域。 (用浅蓝色突出显示)

我试过了 a solution that works , 但它要求我明确指定图像高度,我尽量不这样做,因为来源可以是多种多样的,我需要图像根据卡片宽度灵活地放大/缩小(具有相同的纵横比)。

截图如下:

enter image description here

这是代码 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/

相关文章:

android - jsonArrayRequest 失败

java - 使用 Android 卡 View 导致应用程序崩溃

android - 如何使用 registerReceiver 方法?

android - 锚定到应用栏的 FAB 不会在向上滚动时隐藏

android 两个手指放大/缩小不起作用

android - RecyclerView with CardView Items, notifyItemChanged 和阴影问题

java - Android:如何让 Cardviews 在 Recyclerview 中重叠?

imageview - 调整图像大小以适合父节点

android - ImageView - 如何以编程方式膨胀或添加?

java - 无法 Intent 下一个布局