可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using a web view in which I am adding an image view. How can I set the background of this image view to transparent?
I have tried this:
mImageview.setBackgroundResource(R.color.trans);
Where trans
→ <color name=\"trans\">#00000000 </color>
.
回答1:
In your XML set the Background
attribute to any colour, White(#FFFFFF)
shade or Black(#000000)
shade. If you want transparency, just put 80 before the actual hash code:
#80000000
This will change any colour you want to a transparent one.. :)
回答2:
You can set the background transparent of any layout, any view, or any component by adding this code in XML:
android:background=\"@android:color/transparent\"
回答3:
In addition to what Harshad mentioned:
Two hexadecimal characters can be appended to any hexadecimal color code. The first two characters in an 8-digit hex color code represents its opacity in Android.
The two hexadecimal characters can range from 00 to FF. For example,
- Normal opaque black hex- \"#000000\"
- Fully transparent - \"#00000000\"
- Fully opaque - \"#FF000000\"
- 50% transparent - \"#7F000000\"
This way you can change any color to any level of transparency.
To find the hexadecimal prefix from a percentage:
Divide the percentage number by 100 and multiply by 255 to get the decimal value. Convert the decimal to hexadecimal here.
For example, for 50%, 50/100 * 255 = 127. Using the link we get hexadecimal value 7F.
Source: Android: how to create a transparent or opaque background
回答4:
If you want to add 20% or 30% transparency, you should pre-pend two more characters to the hexadecimal code, like CC.
Note
android:background=\"#CCFF0088\"
in XML
where CC is the alpha value, FF is the red factor, 00 is the green factor, and 88 is the blue factor.
Some opacity code:
Hex Opacity Values
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
You can also set opacity programmatically like:
yourView.getBackground().setAlpha(127);
Set opacity between 0 (fully transparent) to 255 (completely opaque). The 127.5 is exactly 50%.
You can create any level of transparency using the given formula. If you want half transparent:
16 |128 Where 128 is the half of 256.
|8 -0 So it means 80 is half transparent.
And for 25% transparency:
16 |64 Where 64 is the quarter of 256.
|4 -0 So it means 40 is quarter transparent.
回答5:
Use the below code for black:
<color name=\"black\">#000000</color>
Now if you want to use opacity then you can use the below code:
<color name=\"black\">#99000000</color>
And the below for opacity code:
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
回答6:
There is already a predefined constant. Use Color.TRANSPARENT
.
回答7:
There is already a transparent built into Android: R.color.transparent. http://developer.android.com/reference/android/R.color.html#transparent
But I think you may want to make the background of the image that you are placing into the WebView transparent, for example, with a transparent PNG, rather than the ImageView background. If the actual image is not at all see-through then the ImageView background can\'t be seen through it.
回答8:
use this code for transparent color
android:background=\"@android:color/transparent\"
回答9:
In case you want it in code, just:
mComponentName.setBackgroundColor(Color.parseColor(\"#80000000\"));
回答10:
Or, as an alternate, parse the resource ID with the following code:
mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));
回答11:
For those who are still facing this problem, you may try this
element.getBackground().setAlpha(0);
回答12:
One more simple way:
mComponentName.setBackgroundResource(android.R.color.transparent);
回答13:
Use the following for complete transparency:
#00000000
When I tried with #80000000 I got a black transparent overlay which I don\'t want. Try to change the first two digits; it controls the level of transparency, like
#00000000
#10000000
#20000000
#30000000
回答14:
In your XML file, set an attribute \"Alpha\"
such as
android:alpha=\"0.0\" // for transparent
android:alpha=\"1.0\" // for opaque
You can give any value between 0.0 to 1.0 in decimal to apply the required transparency. For example, 0.5 transparency is ideal for disabled component
回答15:
Try this code :)
Its an fully transparent hexa code - \"#00000000\"
回答16:
Another working option I came across is to set android:background=\"@null\"
回答17:
You could also use View.setAlpha(float)
to change the visibility precisely.
0 would be transparent, 1 fully visible. ;)
回答18:
In Android Studio it is very simple to adjust color and opacity using a built-in tool:
回答19:
If you use a drawable XML image you can make it transparent as shown in the picture below, or you can use a color code:
<color name=\"black\">#99000000</color>
回答20:
Try to use the following code. It will help you in full or more.
A .xml file designed to use this code to set background color:
android:background=\"#000000\"
or
android:background=\"#FFFFFF\"
Or you can set it programmatically as well.
Also you can use this code programmatically:
image.setBackgroundDrawable(getResources().getDrawable(
R.drawable.llabackground));
Also this code for setting the background color as well programmatically:
image.setBackgroundColor(Color.parseColor(\"#FFFFFF\"));
This code for the same programmatically:
image.setBackgroundColor(getResources().getColor(Color.WHITE));
The color depends on your choice of which color you want to use for transparent. Mostly use a white or #FFFFFF color.
Regarding R.drawable.llabackground
: This line of code is for your style of the background, like something special or different for your purpose. You can also use this.
回答21:
ImageView.setBackground(R.drawable.my_background);
ImageView.setBackgroundResource(R.color.colorPrimary);
ImageView.getImageAlpha();
ImageView.setAlpha(125); // transparency
回答22:
Try this:
#aa000000
For transparency 000000 = black, you can change these six numbers for the color you want.