我正在使用一个web视图,其中我正在添加一个图像视图。如何将此图像视图的背景设置为透明?

我试过了:

mImageview.setBackgroundResource(R.color.trans);

其中trans→<color name="trans">#00000000 </color>。


当前回答

如果您想增加20%或30%的透明度,您应该在十六进制代码中预先添加两个字符,如CC。

Note

android:background="#CCFF0088

其中CC是alpha值,FF是红色因子,00是绿色因子,88是蓝色因子。

一些不透明代码:

十六进制不透明度值

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5%  — 0D
0% —  00

你也可以像这样通过编程来设置不透明度:

yourView.getBackground().setAlpha(127);

将不透明度设置在0(完全透明)到255(完全不透明)之间。127.5正好是50%。

您可以使用给定的公式创建任何级别的透明度。如果你想要半透明:

 16 |128          Where 128 is the half of 256.
    |8 -0         So it means 80 is half transparent.

对于25%的透明度:

16 |64            Where 64 is the quarter of 256.
   |4 -0          So it means 40 is quarter transparent.

其他回答

带有透明信息的颜色定义可以在窗体中

#AARRGGBB或#ARGB。

你也可以使用更短的值来表示完全透明: # 0000。

其他值包括:

white  grey   black
#FFFF  #F888  #F000 - full color
#EFFF  #E888  #E000
#DFFF  #D888  #D000
#CFFF  #C888  #C000
#BFFF  #B888  #B000
#AFFF  #A888  #A000
#9FFF  #9888  #9000
#8FFF  #8888  #8000
#7FFF  #7888  #7000
#6FFF  #6888  #6000
#5FFF  #5888  #5000
#4FFF  #4888  #4000
#3FFF  #3888  #3000
#2FFF  #2888  #2000
#1FFF  #1888  #1000
#0FFF  #0888  #0000 - full transparency

我遇到的另一个工作选项是设置android:background="@null"

如果您想增加20%或30%的透明度,您应该在十六进制代码中预先添加两个字符,如CC。

Note

android:background="#CCFF0088

其中CC是alpha值,FF是红色因子,00是绿色因子,88是蓝色因子。

一些不透明代码:

十六进制不透明度值

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5%  — 0D
0% —  00

你也可以像这样通过编程来设置不透明度:

yourView.getBackground().setAlpha(127);

将不透明度设置在0(完全透明)到255(完全不透明)之间。127.5正好是50%。

您可以使用给定的公式创建任何级别的透明度。如果你想要半透明:

 16 |128          Where 128 is the half of 256.
    |8 -0         So it means 80 is half transparent.

对于25%的透明度:

16 |64            Where 64 is the quarter of 256.
   |4 -0          So it means 40 is quarter transparent.

如果你使用一个可绘制的XML图像,你可以使它透明如下图所示,或者你可以使用一个颜色代码:

<color name="black">#99000000</color> 

通过在XML中添加以下代码,您可以设置任何布局、任何视图或任何组件的背景透明:

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