【问题标题】:Android Studio background color are included in png imageAndroid Studio 背景颜色包含在 png 图像中
【发布时间】:2019-03-11 11:20:23
【问题描述】:

我在 photoshop cs6 中设计了一个按钮并将其保存为 png 图像。当尝试将它作为 imageView 或 imageButton 添加到 android studio 中时,它包括背景颜色。我试过android:backgroud="@android:color/transparent",但没有运气。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


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

    <EditText
        android:id="@+id/screenTextView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:background="#BDBDBD"
        android:ems="10"
        android:fadingEdge="none"
        android:hint="0"
        android:inputType="none"
        android:textAlignment="viewEnd"
        android:textSize="30sp" />
</LinearLayout>

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

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="0dp"
        android:layout_height="160dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        app:srcCompat="@drawable/buttonn1" />
</LinearLayout>

编辑:https://github.com/Gamer1989/project/issues/1 你可以从这里看到图片。

【问题讨论】:

  • 发布图像或 XML 预览。我认为您只需要在 Photoshop 中更改它。
  • 在 Photoshop 中,尝试“删除背景”橡皮擦
  • 我添加了我的 xml 代码,但无法上传图片。在 photoshop 中,我的图像背景看起来是透明的。
  • U 正在使用 app:srcCompat= 来调用 png,但这对于矢量来说是特殊的。对于 png 你应该使用 android:src=,如果这有效果,请检查。
  • 您可能会看到 ImageButton 的样式设置的背景。您可以只使用 ImageView 而不是 ImageButton。 ImageView 呈现您的图像没有按钮样式,并且具有相同的 onClick 事件

标签: android android-layout android-button


【解决方案1】:

这对我有用。如果它不适合你,你应该考虑你的 png 有问题。您是否使用导入器将图像添加到您的项目?例如 Batch Drawable Importer。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


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

        <EditText
            android:id="@+id/screenTextView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_weight="1"
            android:background="#BDBDBD"
            android:ems="10"
            android:fadingEdge="none"
            android:hint="0"
            android:inputType="none"
            android:textAlignment="viewEnd"
            android:textSize="30sp" />
    </LinearLayout>

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

        <ImageView
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:padding="20dp"
            android:src="@drawable/buttonn1" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 我使用了批量导入器,但还是一样。在 Photoshop 中,我的图像背景是透明的。有一层是透明的,我将它添加到我的按钮上。然后将其保存为 png 文件。
  • 好的,你可以将你的项目推送到github,这样我们就可以看到整个代码了。
  • 好的,导入了你的代码,但我没有这个问题,我没有背景颜色就可以正常工作。我有 Android Studio 3.3.1。考虑到问题出在你的环境中。
  • 你的 png 中有这么多空白空间有什么原因吗?在我看来这是不必要的,你可以让 png 和你的按钮一样大,然后你可能不会遇到这个问题。
【解决方案2】:

这意味着 png 文件没有透明背景。确保引用的文件确实具有此功能。 这个link 教你怎么做。

【讨论】:

  • 当我在 photoshop 中打开图片时,背景不是白色,它是透明的。
  • 在 ImageView 中添加以下行:android:alpha="0.0" PS:0.0 完全透明,1.0 完全不透明
  • 这一次按钮完全消失
  • 无法上传图片。它说你需要 10 声望
  • 我使用 android studio 3.3.2。你可以在这里看到 Photoshop 图片github.com/Gamer1989/project/issues/1
猜你喜欢
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2018-02-23
  • 2023-04-10
相关资源
最近更新 更多