如何添加没有白色背景的图像?

How to add Image without this white background?

enter image description here

这是一个XML文件,

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="#fff2f2"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="12dp"
app:cardElevation="8dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/tick"
        android:id="@+id/image2"/>
</LinearLayout>

和Mainactivity.java, item2List.add(新项目2(R.drawable.checked));

设置 app:cardBackgroundColor="@android:color/transparent" 而不是 app:cardBackgroundColor="#fff2f2"

这将使用您的图片作为背景,而不是强制使用白色背景。

CardView 的默认颜色是白色。如果您想将其更改为其他颜色,请使用以下代码。

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/white">

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

或者您可以通过将 cardBackgroundColor 设置为

将 cardBackgroundColor 设置为透明(如果您不需要任何颜色)
app:cardBackgroundColor="@android:color/transparent"

在这里,我不知道你的图片是 .png 还是 .jpeg,但如果你的图片不是 .png,那么就拍摄 .png 图片,如果你已经拍摄了 .png 图片,那么将 cardview 背景设置为透明。

此处,使用cardBackgroundColor属性去除颜色并设置透明,使用cardElevation属性去除阴影。

请尝试以下:-

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    app:cardElevation="0dp"
    app:cardBackgroundColor="@android:color/transparent" > 

如果您使用 API 级别 21 和更高级别,如果 cardBackgroundColor 无法正常工作,您可以使用 android:backgroundTint。就我而言,它有效。

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        app:cardElevation="0dp"
        android:backgroundTint="@android:color/transparent">