显示白色背景的对话,当使用与透明度的png作为背景时

问题描述 投票:0回答:2

我为Gimp创建了一个Dialog背景,它是一个透明且透明背景的PNG。如果我在ImageView上显示它很好,背景是透明的。

<ImageView
    android:src="@drawable/test"

但是,如果我将其显示为对话框的LinearLayout的背景,则会显示白色背景而不是图像的透明度(Alpha通道)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/test"

final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.xmldialog);

这是PNG:

enter image description here

当我用作对话的LinearLayout的背景时,应该怎样做才能获得alpha通道而不是我创建的图像下方的白色背景?我确定这是对话框的问题。必须将某些东西设置为true。

谢谢。

android android-layout png transparency android-dialog
2个回答
1
投票

两个十六进制字符可以附加到任何十六进制颜色代码。 8位十六进制颜色代码中的前两个字符表示其在Android中的不透明度。

两个十六进制字符的范围为00到FF。例如,

- >普通不透明黑色六角 - “#000000”

- >完全透明 - “#00000000”

- >完全不透明 - “#FF000000”

- > 50%透明 - “#7F000000”

这样,您可以将任何颜色更改为任何透明度级别。

要从百分比中查找十六进制前缀:

将百分比数除以100并乘以255得到小数值。将小数转换为十六进制。

例如,对于50%,50/100 * 255 = 127.使用该链接我们得到十六进制值7F。

希望这有助于您了解为任何视图设置透明背景。


1
投票

尝试将其设置为png作为对话框setBackgroundDrawable()的背景。

  dialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.ic_launcher));

如果您要将设置背景设置为根布局。然后将背景设置为透明。

 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

并使您的根布局保持背景。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/test"

确保使用的png没有在边框上留下任何彩色填充。

您还可以使用指定的主题设置对话框背景。下面是一个例子。

<style name="DialogTheme" parent="android:Theme.Holo.Dialog.NoActionBar">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>

在Dialog的构造函数中使用此主题。

Dialog dialog = new Dialog(this, R.style.DialogTheme); 

根据需要更改主题,并注意使用的属性。

© www.soinside.com 2019 - 2024. All rights reserved.