为什么ImageView的png图像中包含背景颜色?

Why background color are included in png image of ImageView?

我绝对是 Android 的初学者。我正在学习如何设计布局。但是我在使用 ImageView 时遇到问题:

当我使用 PNG 文件设置 ImageView 的 src 时,深色背景会自动添加到所有图像,如屏幕截图所示:

真实形象是这样的:(没有背景)

这是我的工具栏的 xml 布局,我没有为 ImageView 设置任何背景:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:background="?attr/colorPrimaryDark">
        <ImageButton
            android:id="@+id/btn_open_sidebar"
            android:src="@drawable/open_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageButton
            android:layout_alignParentRight="true"
            android:id="@+id/btn_calendar"
            android:src="@drawable/calendar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
</RelativeLayout>

那么如何使 PNG 图像透明或与工具栏具有相同的背景?我的意思是,如何删除默认的深色背景颜色?

如果您使用的是 Android Studio,则不要复制和粘贴 .png 文件,而是右键单击 drawable 和 select New -> ImageAsset,然后 select 图像你想在 drawable 文件夹中。它将去除图像的深色背景部分。

在资源管理器中打开图像,右键单击并转到 "properties",转到详细信息选项卡并检查位深度。根据我的经验 android 如果不是“32”,工作室会添加黑色背景。我不知道它为什么这样做,但它不应用透明度。 我只是用 "paint.net" 编辑图像并将其重新保存为 32 位。 这是一种解决方法,但每次都对我有用。

我认为是因为您正在使用 ImageButton android 添加默认背景。 您可以将 android:backgroud="@android:color/transparent" 添加到 ImageButton 并强制 android 使用透明背景。

您必须更改父布局背景颜色。

这个

android:background="?attr/colorPrimaryDark"

aandroid:背景="any colour code"

您可以将图像视图的背景颜色设置为"#00000000" 它应该可以解决问题。 第2个hexa-digits中的第8个hexa-digits中的颜色标记不透明度 00 是完全透明的,FF 是完全不透明的。

它是 ImageButton 的默认 属性。这不会在 ImageView 中发生,因此请更新您的问题。使用 ImageView 或将 ImageButton 的背景设置为空或透明颜色来修复它

    android:background="@android:color/transparent"

在你的相对布局中你有这样的背景: android:background="?attr/colorPrimaryDark" 在 colors.xlm

中检查此颜色