【问题标题】:How to get rid of blank spaces around an ImageView inside a CardView如何摆脱 CardView 内 ImageView 周围的空白
【发布时间】:2016-12-21 01:41:45
【问题描述】:

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

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

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

截图如下:

这里是sn-p的代码:

<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 自动环绕图像(不留任何空白)并且不明确指定图像高度?

【问题讨论】:

    标签: android imageview cardview


    【解决方案1】:

    &lt;RelativeLayout&gt; 的高度更改为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>
    

    【讨论】:

    • 试过了,但您的解决方案不起作用。顺便说一句,我更改了屏幕截图,因为前面显示错误的浅蓝色突出显示。
    • 愚蠢的安卓系统。无论如何,他们怎么能将一些奇怪的配置作为默认配置。 ://
    【解决方案2】:

    尝试将 scaleType 更改为 android:scaleType="centerCrop" 或其他一些 scaleType。
    见这里:
    https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

    【讨论】:

    • 这也是一个很好的选择。谢谢你提到它!
    猜你喜欢
    • 2014-08-17
    • 2014-08-15
    • 1970-01-01
    • 2011-04-17
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多