在Android中删除ImageView边框

时间:2015-01-28 00:08:50

标签: android imageview

我正在制作一个使用卡片来显示信息的应用程序,我希望在卡片的左侧有一个图像。我遇到的问题是,由于某种原因,在左侧插入的ImageView我的所有侧面都被填充。

在网上搜索之后,我发现stackoverflow上的很多帖子都在问同一个问题,所有人都被告知使用这段代码修复他们的问题:

android:adjustViewBounds="true"

并简单地在xml中的ImageView中添加它。我试过这个,但填充物仍然存在。我已将图像视图的背景颜色设置为浅绿色,以说明填充区域。

以下是该卡的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="2dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="5dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/drinkImage"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:background="#00FF00"
            android:gravity="left"
            />

        <TextView
            android:padding="8dp"
            android:id="@+id/drinkName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_toRightOf="@+id/drinkImage"
            />

        <TextView
            android:padding="8dp"
            android:id="@+id/drinkDesc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/drinkName"
            android:layout_toRightOf="@+id/drinkImage"
            />

    </RelativeLayout>
</android.support.v7.widget.CardView>

这就是我在屏幕上看到的内容:

http://i.stack.imgur.com/bVaR4.png

enter image description here

2 个答案:

答案 0 :(得分:1)

绿色&#34;边界&#34;不是边框,是您ImageView设置的android:background="#00FF00"的背景。在ImageView上方绘制您的图片。图像比率不适合ImageView的宽度和高度,或图像宽度或高度小于ImageView的宽度或高度。在这种情况下,ImageView的默认行为是将图像置于ImageView中心。因此你看到了空间。您可以使用android:scaleType指定行为。请参阅official documentation。否则,您可以调整ImageView的宽度和高度以匹配图像的宽度和高度。

更新: 哦,对不起,我没有注意到你正在使用CardView

根据文档,这是按照设计的:

  

由于圆角切割的昂贵性质,在L之前的平台上,CardView不会剪切与圆角相交的子节点。相反,它添加了填充以避免这种交集(请参阅setPreventCornerOverlap(boolean)以更改此行为)。

有关详细信息,请参阅the CardView docs

答案 1 :(得分:0)

好的,事实证明我是通过右键单击drawable /并单击new-&gt; Image Asset将图像资源添加到项目中。我没有意识到这实际上是裁剪和调整图像大小,因为我显然不知道图像资产创建者的正确使用。

要将.PNG文件添加到项目中,我只需从Finder(相当于Windows资源管理器的Mac)中复制图像文件并将它们粘贴到Android Studio中的drawables文件夹中,一切都很完美。这样的菜鸟错误!