【问题标题】:Why background color are included in png image of ImageView?为什么 ImageView 的 png 图像中包含背景颜色?
【发布时间】:2016-01-27 10:09:17
【问题描述】:

我是 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:backgroud="@android:color/transparent" 添加到您的ImageButton s
  • 谢谢。有效。请张贴您的答案。我会把它绿化。

标签: android android-layout android-imageview android-image


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          这个

          android:background="?attr/colorPrimaryDark"

          aandroid:background="任何颜色代码"

          【讨论】:

            【解决方案6】:

            可以将imageview的背景颜色设置为"#00000000" 它应该可以解决问题。 颜色标记不透明度中的 8 个六位数字中的前两位六位数字 00 是完全透明的,FF 是完全不透明的。

            【讨论】:

              【解决方案7】:

              在您的相对布局中,您有以下背景: android:background="?attr/colorPrimaryDark" 在colors.xlm中检查这个颜色

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-01-22
                • 1970-01-01
                • 2020-07-15
                • 1970-01-01
                • 2018-02-23
                • 2023-04-10
                相关资源
                最近更新 更多